William Jiang

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

jQuery examples summary 4

Continue the summary, the examples are main from http://api.jquery.com/.

Continue the jQuery examples summary

//1. clone( [ withDataAndEvents ] )
Create a deep copy of the set of matched elements.

$("b").clone().prependTo("p");
$(INPUT).clone().val('').appendTo(CONTAINER);
$(selector).clone().removeClass('unique');

//2. fadeOut( [ duration ], [ callback ] )
Hide the matched elements by fading them to transparent.

//3. empty()
Remove all child nodes of the set of matched elements from the DOM. $(‘.hello’).empty();

//4. remove( [ selector ] )
Remove the set of matched elements from the DOM.
Similar to .empty(), the .remove() method takes elements out of the DOM. Use .remove() when you want to remove the element itself, as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed. To remove the elements without removing data and events, use .detach() instead.

$('div').remove('.hello');
$("button").click(function () {
 $("p").remove(":contains('Hello')");
});
$("select#mySelect option[value='option1']").remove();
$("select#mySelect option[selected='true']").remove();
$('#mySelect :selected').remove();

About empty() and remove():

Due to the way jQuery handles remove vs empty, empty is thousands of times faster, at least in this situation.
So do this:

$('#container').empty().remove();

…instead of this:

$('#container').remove();

Using empty() before remove() is much faster and safer.

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: