深入学习jquery源码之empty()与返回值remove()

empty()

概述

删除匹配的元素集合中所有的子节点。

把所有段落的子元素(包括文本节点)删除

<p>Hello, <span>Person</span> <a href="#">and person</a></p>
$("p").empty();
<p></p>

 

remove([expr])

概述

从DOM中删除所有匹配的元素。

这个方法不会把匹配的元素从jQuery对象中删除,因而可以在将来再使用这些匹配的元素。但除了这个元素本身得以保留之外,其他的比如绑定的事件,附加的数据等都会被移除。

参数

expr String

用于筛选元素的jQuery表达式

从DOM中把所有段落删除

<p>Hello</p> how are <p>you?</p>
$("p").remove();
how are

从DOM中把带有hello类的段落删除

<p class="hello">Hello</p> how are <p>you?</p>
$("p").remove(".hello");
how are <p>you?</p>

 

jquery源码

/**
* Determines whether an object can have data
*/
jQuery.acceptData = function (elem) {
var noData = jQuery.noData[(elem.nodeName + " ").toLowerCase()],
nodeType = +elem.nodeType || 1;

// Do not set data on non-element DOM nodes because it will not be cleared (#8335).
return nodeType !== 1 && nodeType !== 9 ?
false :

// Nodes accept data unless otherwise specified; rejection can be conditional
!noData || noData !== true && elem.getAttribute("classid") === noData;
};

jQuery.extend({
// Unique for each copy of jQuery on the page
expando: "jQuery" + (version + Math.random()).replace(/\D/g, ""),
now: function () {
return +(new Date());
},

// jQuery.support is not used in Core but other projects attach their
// properties to it so it needs to exist.
support: support
});


jQuery.extend({
cleanData: function (elems, /* internal */ acceptData) {
var elem, type, id, data,
i = 0,
internalKey = jQuery.expando,
cache = jQuery.cache,
deleteExpando = support.deleteExpando,
special = jQuery.event.special;

for (; (elem = elems[i]) != null; i++) {
if (acceptData || jQuery.acceptData(elem)) {

id = elem[internalKey];
data = id && cache[id];

if (data) {
if (data.events) {
for (type in data.events) {
if (special[type]) {
jQuery.event.remove(elem, type);

// This is a shortcut to avoid jQuery.event.remove's overhead
} else {
jQuery.removeEvent(elem, type, data.handle);
}
}
}

// Remove cache only if it was not already removed by jQuery.event.remove
if (cache[id]) {

delete cache[id];

// IE does not allow us to delete expando properties from nodes,
// nor does it have a removeAttribute function on Document nodes;
// we must handle all of these cases
if (deleteExpando) {
delete elem[internalKey];

} else if (typeof elem.removeAttribute !== strundefined) {
elem.removeAttribute(internalKey);

} else {
elem[internalKey] = null;
}

deletedIds.push(id);
}
}
}
}
}
});

function getAll(context, tag) {
var elems, elem,
i = 0,
found = typeof context.getElementsByTagName !== strundefined ? context.getElementsByTagName(tag || "*") :
typeof context.querySelectorAll !== strundefined ? context.querySelectorAll(tag || "*") :
undefined;

if (!found) {
for (found = [], elems = context.childNodes || context; (elem = elems[i]) != null; i++) {
if (!tag || jQuery.nodeName(elem, tag)) {
found.push(elem);
} else {
jQuery.merge(found, getAll(elem, tag));
}
}
}

return tag === undefined || tag && jQuery.nodeName(context, tag) ?
jQuery.merge([context], found) :
found;
}

jQuery.fn.extend({
remove: function (selector, keepData /* Internal Use Only */) {
var elem,
elems = selector ? jQuery.filter(selector, this) : this,
i = 0;

for (; (elem = elems[i]) != null; i++) {

if (!keepData && elem.nodeType === 1) {
jQuery.cleanData(getAll(elem));
}

if (elem.parentNode) {
if (keepData && jQuery.contains(elem.ownerDocument, elem)) {
setGlobalEval(getAll(elem, "script"));
}
elem.parentNode.removeChild(elem);
}
}

return this;
},
empty: function () {
var elem,
i = 0;

for (; (elem = this[i]) != null; i++) {
// Remove element nodes and prevent memory leaks
if (elem.nodeType === 1) {
jQuery.cleanData(getAll(elem, false));
}

// Remove any remaining nodes
while (elem.firstChild) {
elem.removeChild(elem.firstChild);
}

// If this is a select, ensure that it displays empty (#12336)
// Support: IE<9
if (elem.options && jQuery.nodeName(elem, "select")) {
elem.options.length = 0;
}
}

return this;
}

});