William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

jQuery examples summary 3

Continue the jQuery examples summary, the examples mainly from http://api.jquery.com/

//1. after()
$('.inner').after('<p>Test</p>');
$('.container').after($('h2'));
var $newdiv1 = $('<div id="object1"/>'),
  newdiv2 = document.createElement('div'),
  existingdiv1 = document.getElementById('foo');
$('p').first().after($newdiv1, [newdiv2, existingdiv1]);

$('<div/>').after('<p></p>').addClass('foo')
  .filter('p').attr('id', 'bar').html('hello')
  .end()
  .appendTo('body');
$("p").after( $("b") );

//2. insertAfter( target )
$('h2').insertAfter($('.container'));
$("p").insertAfter("#foo");

//3. before( content, [ content ] )
$('.inner').before('<p>Test</p>');
$('.container').before($('h2'));
$("p").before("<b>Hello</b>"); $("p").before( document.createTextNode("Hello") );
$("p").before( $("b") );

//4. insertBefore( target )
$('<p>Test</p>').insertBefore('.inner');
$('h2').insertBefore($('.container'));
$("p").insertBefore("#foo");
$("input[type=password]").before(function() {console.log(this);});

What is the difference between insertAfter() and appendTo()?

  • insertAfter() inserts an element AFTER another element,
  • While .appendTo() insert elements INTO another element
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: