<strong>一、jQuery.buildFragment使用方法 <br></strong>1、参数 <br>jQuery.buildFragment( args, context, scripts );2、返回值 <br>return { fragment: fragment, cacheable: cacheable }; <br><strong>二、思路分析</strong> <br>1、处理context参数 <br>根据传入到context参数值的不同,确保context为文档根节点document <br>2、限制可缓存条件 <br>2.1、字符串小于512字节 <br>2.2、字符串不存在option标签(克隆option标签会丢失选中状态,因此不缓存) <br>2.3、字符串不存在<object>,<embed>标签(IE 6不能把<object>,<embed>标签嵌入到文档碎片中) <br>2.4、字符串不存在checked属性(只针对克隆拥有checked属性节点时丢失选中状态的浏览器,如:Safria) <br>2.5、字符串中不存在html5标签(只针对不支持html5标签的浏览器) <br>3、处理args数组 <br>通过jQuery.clean函数格式化处理数组项字符串,并生成dom节点添加到文档碎片中 <br>4、判断缓存值 <br>如果缓存值为由clean函数处理的文档碎片,则数组项字符串略过clean函数处理 <br>5、返回值 <br>函数返回一个对象,保存文档碎片和可缓存状态 <br><strong>三、源码注释分析</strong> <br>【基于jQuery1.8.3】 <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="88177" class="copybut" id="copybut88177" onclick="doCopy('code88177')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code88177"> <br>var rnocache = /rchecked = /checked\s*(?:[^=]|=\s*.checked.)/i, <br>rnoshimcache = new RegExp("]", "i"); <br>jQuery.fragments = {}; <br>jQuery.buildFragment = function( args, context, scripts ) { <br>var fragment, cacheable, cachehit, <br>first = args[ 0 ]; <br>// Set context from what may come in as undefined or a jQuery collection or a node <br>// Updated to fix #12266 where accessing context[0] could throw an exception in IE9/10 & <br>// also doubles as fix for #8950 where plain objects caused createDocumentFragment exception <br>// 根据参数context值的不同,确保context为文档根节点document <br>// jQuery1.8.0版本以后代码相对于之前版本有很大改进,以下是改进地方: <br>// 针对context参数值为undefined, jQuery对象,DOM元素节点情况改进代码 <br>// 解决了1.8.0版本中context参数为文档片段(#document-fragment)的bug <br>context = context || document; <br>context = !context.nodeType && context[0] || context; <br>context = context.ownerDocument || context; <br>// Only cache "small" (1/2 KB) HTML strings that are associated with the main document <br>// Cloning options loses the selected state, so don't cache them <br>// IE 6 doesn't like it when you put <object> or <embed> elements in a fragment <br>// Also, WebKit does not clone 'checked' attributes on cloneNode, so don't cache <br>// Lastly, IE6,7,8 will not correctly reuse cached fragments that were created from unknown elems #10501 <br>// html字符串小于512字节 <br>// 克隆option标签会丢失选中状态,因此不缓存 <br>// IE 6不能把<object>,<embed>标签嵌入到文档碎片中 <br>// WebKit浏览器(如:Safria)克隆拥有checked属性节点时,也会丢失选中状态,因此不缓存,google最新版本不存在该bug <br>// 最后,IE6,7、8不会正确地重用由html5标签元素创建的缓存片段 <br>if ( args.length === 1 && typeof first === "string" && first.length first.charAt(0) === "// 如果浏览器能够克隆checked属性和支持html5,或者html字符串中不存在checked和html5标签元素 <br>(jQuery.support.checkClone || !rchecked.test( first )) && <br>(jQuery.support.html5Clone || !rnoshimcache.test( first )) ) { <br>// Mark cacheable and look for a hit <br>// 如果以上条件都满足,则打上可缓存标记 <br>cacheable = true; <br>// 将数组项字符串(主要是html字符串)缓存到jQuery.fragment对象的属性列表中,并获取缓存值 <br>// 如果clean函数已经处理过了第二次相同的字符串内容,缓存值则为clean函数处理的文档碎片,字符串解析可以略过clean处理 <br>fragment = jQuery.fragments[ first ]; <br>// 在clean函数处理了第一次字符串(与第二次相同)后,cachehit为true <br>cachehit = fragment !== undefined; <br>} <br>// 判断缓存值 <br>if ( !fragment ) { <br>fragment = context.createDocumentFragment(); <br>// 通过clean函数处理args数组,将数组每一项字符串都生成dom节点, <br>// 并且添加到提供的文档碎片(fragment)中,因此返回的对象中的fragment属性 <br>// 保存了参数args数组项字符串生成的dom节点 <br>jQuery.clean( args, context, fragment, scripts ); <br>// Update the cache, but only store false <br>// unless this is a second parsing of the same content <br>// 当cachehit为true时,jQuery.fragment[first]为clean函数处理的文档碎片 <br>if ( cacheable ) { <br>jQuery.fragments[ first ] = cachehit && fragment; <br>} <br>} <br>return { fragment: fragment, cacheable: cacheable }; <br>}; <br></embed></object></embed></object> </div> </embed></object></embed></object>