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

jQuery.buildFragment使用方法及思路分析

 更新時(shí)間:2013年01月07日 17:43:28   作者:  
jQuery.buildFragment使用方法與思路分析以及源碼注釋分析等等,感興趣的朋友可以了解下哦
一、jQuery.buildFragment使用方法
1、參數(shù)
jQuery.buildFragment( args, context, scripts );2、返回值
return { fragment: fragment, cacheable: cacheable };
二、思路分析
1、處理context參數(shù)
根據(jù)傳入到context參數(shù)值的不同,確保context為文檔根節(jié)點(diǎn)document
2、限制可緩存條件
2.1、字符串小于512字節(jié)
2.2、字符串不存在option標(biāo)簽(克隆option標(biāo)簽會(huì)丟失選中狀態(tài),因此不緩存)
2.3、字符串不存在<object>,<embed>標(biāo)簽(IE 6不能把<object>,<embed>標(biāo)簽嵌入到文檔碎片中)
2.4、字符串不存在checked屬性(只針對(duì)克隆擁有checked屬性節(jié)點(diǎn)時(shí)丟失選中狀態(tài)的瀏覽器,如:Safria)
2.5、字符串中不存在html5標(biāo)簽(只針對(duì)不支持html5標(biāo)簽的瀏覽器)
3、處理args數(shù)組
通過(guò)jQuery.clean函數(shù)格式化處理數(shù)組項(xiàng)字符串,并生成dom節(jié)點(diǎn)添加到文檔碎片中
4、判斷緩存值
如果緩存值為由clean函數(shù)處理的文檔碎片,則數(shù)組項(xiàng)字符串略過(guò)clean函數(shù)處理
5、返回值
函數(shù)返回一個(gè)對(duì)象,保存文檔碎片和可緩存狀態(tài)
三、源碼注釋分析
【基于jQuery1.8.3】
復(fù)制代碼 代碼如下:

var rnocache = /<(?:script|object|embed|option|style)/i,
rchecked = /checked\s*(?:[^=]|=\s*.checked.)/i,
rnoshimcache = new RegExp("<(?:" + nodeNames + ")[\\s/>]", "i");
jQuery.fragments = {};
jQuery.buildFragment = function( args, context, scripts ) {
var fragment, cacheable, cachehit,
first = args[ 0 ];
// Set context from what may come in as undefined or a jQuery collection or a node
// Updated to fix #12266 where accessing context[0] could throw an exception in IE9/10 &
// also doubles as fix for #8950 where plain objects caused createDocumentFragment exception
// 根據(jù)參數(shù)context值的不同,確保context為文檔根節(jié)點(diǎn)document
// jQuery1.8.0版本以后代碼相對(duì)于之前版本有很大改進(jìn),以下是改進(jìn)地方:
// 針對(duì)context參數(shù)值為undefined, jQuery對(duì)象,DOM元素節(jié)點(diǎn)情況改進(jìn)代碼
// 解決了1.8.0版本中context參數(shù)為文檔片段(#document-fragment)的bug
context = context || document;
context = !context.nodeType && context[0] || context;
context = context.ownerDocument || context;
// Only cache "small" (1/2 KB) HTML strings that are associated with the main document
// Cloning options loses the selected state, so don't cache them
// IE 6 doesn't like it when you put <object> or <embed> elements in a fragment
// Also, WebKit does not clone 'checked' attributes on cloneNode, so don't cache
// Lastly, IE6,7,8 will not correctly reuse cached fragments that were created from unknown elems #10501
// html字符串小于512字節(jié)
// 克隆option標(biāo)簽會(huì)丟失選中狀態(tài),因此不緩存
// IE 6不能把<object>,<embed>標(biāo)簽嵌入到文檔碎片中
// WebKit瀏覽器(如:Safria)克隆擁有checked屬性節(jié)點(diǎn)時(shí),也會(huì)丟失選中狀態(tài),因此不緩存,google最新版本不存在該bug
// 最后,IE6,7、8不會(huì)正確地重用由html5標(biāo)簽元素創(chuàng)建的緩存片段
if ( args.length === 1 && typeof first === "string" && first.length < 512 && context === document &&
first.charAt(0) === "<" && !rnocache.test( first ) &&
// 如果瀏覽器能夠克隆checked屬性和支持html5,或者h(yuǎn)tml字符串中不存在checked和html5標(biāo)簽元素
(jQuery.support.checkClone || !rchecked.test( first )) &&
(jQuery.support.html5Clone || !rnoshimcache.test( first )) ) {
// Mark cacheable and look for a hit
// 如果以上條件都滿足,則打上可緩存標(biāo)記
cacheable = true;
// 將數(shù)組項(xiàng)字符串(主要是html字符串)緩存到j(luò)Query.fragment對(duì)象的屬性列表中,并獲取緩存值
// 如果clean函數(shù)已經(jīng)處理過(guò)了第二次相同的字符串內(nèi)容,緩存值則為clean函數(shù)處理的文檔碎片,字符串解析可以略過(guò)clean處理
fragment = jQuery.fragments[ first ];
// 在clean函數(shù)處理了第一次字符串(與第二次相同)后,cachehit為true
cachehit = fragment !== undefined;
}
// 判斷緩存值
if ( !fragment ) {
fragment = context.createDocumentFragment();
// 通過(guò)clean函數(shù)處理args數(shù)組,將數(shù)組每一項(xiàng)字符串都生成dom節(jié)點(diǎn),
// 并且添加到提供的文檔碎片(fragment)中,因此返回的對(duì)象中的fragment屬性
// 保存了參數(shù)args數(shù)組項(xiàng)字符串生成的dom節(jié)點(diǎn)
jQuery.clean( args, context, fragment, scripts );
// Update the cache, but only store false
// unless this is a second parsing of the same content
// 當(dāng)cachehit為true時(shí),jQuery.fragment[first]為clean函數(shù)處理的文檔碎片
if ( cacheable ) {
jQuery.fragments[ first ] = cachehit && fragment;
}
}
return { fragment: fragment, cacheable: cacheable };
};

相關(guān)文章

最新評(píng)論