jQuery examples summary 4
Blogs20112011-08-09
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.
