欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery3.0中的buildFragment私有函數(shù)詳解

 更新時間:2016年08月16日 09:43:59   作者:snandy  
在 jQuery3.0中,buildFragment 是一個私有函數(shù),用來構(gòu)建一個包含子節(jié)點 fragment 對象。下文給大家介紹jQuery3.0中的buildFragment私有函數(shù)詳解,對jquery3.0 buildfragment相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧

時隔 3 個月,jQuery 團(tuán)隊終于發(fā)布了 3.0 Alpha 版本。有兩個版本 jQuery compat 3.0 和 jQuery 3.0。

jQuery compat 3.0 對應(yīng)之前的 1.x, 兼容更多的瀏覽器,對于IE支持到 8.0 版本

jQuery 3.0 對應(yīng)之前的 2.x,關(guān)注更新的瀏覽器,對于IE支持到 9.0 版本

此外, 3.0還增加了對 Yandex 瀏覽器的支持,一款來自俄羅斯的瀏覽器。

下面看下jQuery3.0中的buildFragment。

在 jQuery3.0中,buildFragment 是一個私有函數(shù),用來構(gòu)建一個包含子節(jié)點 fragment 對象。這個 fragment 在 DOM1 中就已經(jīng)有了,所有瀏覽器都支持。當(dāng)頻繁操作(添加、插入) DOM 時使用該方法可以提高性能,John resig 做過一個測試及一篇博客。

jQuery3.0 中 buildFragment 只在 domManip 和 jQuery.parseHTML 中使用,domManip 則被 DOM 操作如 append、prepend、before、after 等方法的所依賴。

如下圖

buildFragment 函數(shù)有 5 個參數(shù),源碼如下

function buildFragment( elems, context, scripts, selection, ignored ) {
var elem, tmp, tag, wrap, contains, j,
fragment = context.createDocumentFragment(),
nodes = [],
i = 0,
l = elems.length;
for ( ; i < l; i++ ) {
elem = elems[ i ];
if ( elem || elem === 0 ) {
// Add nodes directly
if ( jQuery.type( elem ) === "object" ) {
// Support: Android <=4.0 only, PhantomJS 1 only
// push.apply(_, arraylike) throws on ancient WebKit
jQuery.merge( nodes, elem.nodeType ? [ elem ] : elem );
// Convert non-html into a text node
} else if ( !rhtml.test( elem ) ) {
nodes.push( context.createTextNode( elem ) );
// Convert html into DOM nodes
} else {
tmp = tmp || fragment.appendChild( context.createElement( "div" ) );
// Deserialize a standard representation
tag = ( rtagName.exec( elem ) || [ "", "" ] )[ 1 ].toLowerCase();
wrap = wrapMap[ tag ] || wrapMap._default;
tmp.innerHTML = wrap[ 1 ] + jQuery.htmlPrefilter( elem ) + wrap[ 2 ];
// Descend through wrappers to the right content
j = wrap[ 0 ];
while ( j-- ) {
tmp = tmp.lastChild;
}
// Support: Android <=4.0 only, PhantomJS 1 only
// push.apply(_, arraylike) throws on ancient WebKit
jQuery.merge( nodes, tmp.childNodes );
// Remember the top-level container
tmp = fragment.firstChild;
// Ensure the created nodes are orphaned (#12392)
tmp.textContent = "";
}
}
}
// Remove wrapper from fragment
fragment.textContent = "";
i = 0;
while ( ( elem = nodes[ i++ ] ) ) {
// Skip elements already in the context collection (trac-4087)
if ( selection && jQuery.inArray( elem, selection ) > -1 ) {
if ( ignored ) {
ignored.push( elem );
}
continue;
}
contains = jQuery.contains( elem.ownerDocument, elem );
// Append to fragment
tmp = getAll( fragment.appendChild( elem ), "script" );
// Preserve script evaluation history
if ( contains ) {
setGlobalEval( tmp );
}
// Capture executables
if ( scripts ) {
j = 0;
while ( ( elem = tmp[ j++ ] ) ) {
if ( rscriptType.test( elem.type || "" ) ) {
scripts.push( elem );
}
}
}
}
return fragment;
}

該方法主要執(zhí)行步驟

通過第二個參數(shù) content 創(chuàng)建 fragment

通過第一個參數(shù) elems 構(gòu)建 nodes ,將 elems 內(nèi)元素轉(zhuǎn)成 DOM 元素存放于數(shù)組 nodes 中

將 nodes 里元素循環(huán)放入添加到文檔碎片 fragment 上

返回 fragment

重點在第 2 步,構(gòu)建 nodes,有 3 種情形

elem 是 DOM 元素(根據(jù)nodeType判斷),直接放入 nodes 數(shù)組中

elem 是字符串且不是 HTML tag,創(chuàng)建文本節(jié)點對象(textNode),放入 nodes 數(shù)組中

elem 是字符串且是 HTML tag,將其轉(zhuǎn)成 DOM 元素,放入 nodes 數(shù)組中

如圖示

后面的兩個參數(shù)需要注意下

1. 最后兩個參數(shù) selection 和 ignored 只在 replaceWith 方法里使用。需要了解的是 replaceWith 只做節(jié)點替換,不會替換先前元素的所有數(shù)據(jù)(Data),比如綁定事件,$.data 都不會被新元素?fù)碛小?/p>

2. scripts 參數(shù)只在 jQuery.parseHTML 方法里使用(domManip里傳false),當(dāng) jQuery.parseHTML 的第三個參數(shù) keepScripts 為 false 時將刪除節(jié)點里所有的 script tag

以上所述是小編給大家介紹的jQuery3.0中的buildFragment私有函數(shù)詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論