jQuery.buildFragment使用方法及思路分析 - Web前端
作者:98wpeu发布时间:2026-06-08分类:网页前端技术浏览:5
导读:一、JQuery.bUIldFragment使用方法1、参数jQuery.buildFragment(args,context,scripts);2、返回值retur...
一、JQuery.bUIldFragment使用方法
1、参数
jQuery.buildFragment( args, context, scripts );2、返回值
return { fragment: fragment, CACheable: cacheable };
二、思路分析
1、处理context参数
根据传入到context参数值的不同,确保context为文档根节点document
2、限制可缓存条件
2.1、字符串小于512字节
2.2、字符串不存在option标签(克隆option标签会丢失选中状态,因此不缓存)
2.3、字符串不存在<Object>,<embed>标签(ie 6不能把<object>,<embed>标签嵌入到文档碎片中)
2.4、字符串不存在checked属性(只针对克隆拥有checked属性节点时丢失选中状态的浏览器,如:Safria)
2.5、字符串中不存在html5标签(只针对不支持HTML5标签的浏览器)
3、处理args数组
通过jquery.clean函数格式化处理数组项字符串,并生成Dom节点添加到文档碎片中
4、判断缓存值
如果缓存值为由clean函数处理的文档碎片,则数组项字符串略过clean函数处理
5、返回值
函数返回一个对象,保存文档碎片和可缓存状态
三、源码注释分析
【基于jquery1.8.3】
复制代码 代码如下:
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
// 根据参数context值的不同,确保context为文档根节点document
// jQuery1.8.0版本以后代码相对于之前版本有很大改进,以下是改进地方:
// 针对context参数值为undefined, jQuery对象,DOM元素节点情况改进代码
// 解决了1.8.0版本中context参数为文档片段(#document-fragment)的bug
context = context || document;
context = !context.nodeType && context[0] || context;
context = context.ownerDocument || context;
// Only cache "small" (1/2 KB) htmlstrings 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字节
// 克隆option标签会丢失选中状态,因此不缓存
// IE 6不能把<object>,<embed>标签嵌入到文档碎片中
// WebKit浏览器(如:Safria)克隆拥有checked属性节点时,也会丢失选中状态,因此不缓存,google最新版本不存在该bug
// 最后,IE6,7、8不会正确地重用由Html5标签元素创建的缓存片段
if ( args.length === 1 && typeof first === "String" && first.length < 512 && context === document &&
first.charAt(0) === "<" && !rnocache.test( first ) &&
// 如果浏览器能够克隆checked属性和支持html5,或者html字符串中不存在checked和html5标签元素
(jQuery.support.checkClone || !rchecked.test( first )) &&
(jQuery.support.html5Clone || !rnoshimcache.test( first )) ) {
// Mark cacheable and look for a hit
// 如果以上条件都满足,则打上可缓存标记
cacheable = true;
// 将数组项字符串(主要是html字符串)缓存到jQuery.fragment对象的属性列表中,并获取缓存值
// 如果clean函数已经处理过了第二次相同的字符串内容,缓存值则为clean函数处理的文档碎片,字符串解析可以略过clean处理
fragment = jQuery.fragments[ first ];
// 在clean函数处理了第一次字符串(与第二次相同)后,cachehit为true
cachehit = fragment !== undefined;
}
// 判断缓存值
if ( !fragment ) {
fragment = context.createDocumentFragment();
// 通过clean函数处理args数组,将数组每一项字符串都生成dom节点,
// 并且添加到提供的文档碎片(fragment)中,因此返回的对象中的fragment属性
// 保存了参数args数组项字符串生成的dom节点
jQuery.clean( args, context, fragment, scripts );
// UpDate the cache, but only storefalse
// unLessthis is a second parsing of the same content
// 当cachehit为true时,jQuery.fragment[first]为clean函数处理的文档碎片
if ( cacheable ) {
jQuery.fragments[ first ] = cachehit && fragment;
}
}
return { fragment: fragment, cacheable: cacheable };
};
1、参数
jQuery.buildFragment( args, context, scripts );2、返回值
return { fragment: fragment, CACheable: cacheable };
二、思路分析
1、处理context参数
根据传入到context参数值的不同,确保context为文档根节点document
2、限制可缓存条件
2.1、字符串小于512字节
2.2、字符串不存在option标签(克隆option标签会丢失选中状态,因此不缓存)
2.3、字符串不存在<Object>,<embed>标签(ie 6不能把<object>,<embed>标签嵌入到文档碎片中)
2.4、字符串不存在checked属性(只针对克隆拥有checked属性节点时丢失选中状态的浏览器,如:Safria)
2.5、字符串中不存在html5标签(只针对不支持HTML5标签的浏览器)
3、处理args数组
通过jquery.clean函数格式化处理数组项字符串,并生成Dom节点添加到文档碎片中
4、判断缓存值
如果缓存值为由clean函数处理的文档碎片,则数组项字符串略过clean函数处理
5、返回值
函数返回一个对象,保存文档碎片和可缓存状态
三、源码注释分析
【基于jquery1.8.3】
复制代码 代码如下:
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
// 根据参数context值的不同,确保context为文档根节点document
// jQuery1.8.0版本以后代码相对于之前版本有很大改进,以下是改进地方:
// 针对context参数值为undefined, jQuery对象,DOM元素节点情况改进代码
// 解决了1.8.0版本中context参数为文档片段(#document-fragment)的bug
context = context || document;
context = !context.nodeType && context[0] || context;
context = context.ownerDocument || context;
// Only cache "small" (1/2 KB) htmlstrings 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字节
// 克隆option标签会丢失选中状态,因此不缓存
// IE 6不能把<object>,<embed>标签嵌入到文档碎片中
// WebKit浏览器(如:Safria)克隆拥有checked属性节点时,也会丢失选中状态,因此不缓存,google最新版本不存在该bug
// 最后,IE6,7、8不会正确地重用由Html5标签元素创建的缓存片段
if ( args.length === 1 && typeof first === "String" && first.length < 512 && context === document &&
first.charAt(0) === "<" && !rnocache.test( first ) &&
// 如果浏览器能够克隆checked属性和支持html5,或者html字符串中不存在checked和html5标签元素
(jQuery.support.checkClone || !rchecked.test( first )) &&
(jQuery.support.html5Clone || !rnoshimcache.test( first )) ) {
// Mark cacheable and look for a hit
// 如果以上条件都满足,则打上可缓存标记
cacheable = true;
// 将数组项字符串(主要是html字符串)缓存到jQuery.fragment对象的属性列表中,并获取缓存值
// 如果clean函数已经处理过了第二次相同的字符串内容,缓存值则为clean函数处理的文档碎片,字符串解析可以略过clean处理
fragment = jQuery.fragments[ first ];
// 在clean函数处理了第一次字符串(与第二次相同)后,cachehit为true
cachehit = fragment !== undefined;
}
// 判断缓存值
if ( !fragment ) {
fragment = context.createDocumentFragment();
// 通过clean函数处理args数组,将数组每一项字符串都生成dom节点,
// 并且添加到提供的文档碎片(fragment)中,因此返回的对象中的fragment属性
// 保存了参数args数组项字符串生成的dom节点
jQuery.clean( args, context, fragment, scripts );
// UpDate the cache, but only storefalse
// unLessthis is a second parsing of the same content
// 当cachehit为true时,jQuery.fragment[first]为clean函数处理的文档碎片
if ( cacheable ) {
jQuery.fragments[ first ] = cachehit && fragment;
}
}
return { fragment: fragment, cacheable: cacheable };
};
相关推荐
- jquery实现漂浮在网页右侧的qq在线客服插件示例 - Web前端
- Jquery实现鼠标移上弹出提示框、移出消失思路及代码 - Web前端
- jquery实现的一个导航滚动效果具体代码 - Web前端
- jQuery数据缓存功能的实现思路及简单模拟 - Web前端
- jQuery解决下拉框select设宽度时IE 6/7/8下option超出显示不全 - Web前端
- jQuery 过滤方法filter()选择具有特殊属性的元素 - Web前端
- jQuery根据纬度经度查看地图处理程序 - Web前端
- jQuery输入城市查看地图使用介绍 - Web前端
- 用JQuery 判断某个属性是否存在hasAttr的解决方法 - Web前端
- jquery清空textarea等输入框实现代码 - Web前端
- 网页前端技术排行
- 最近发表
-
- WordPress随机显示特色图片插件:Random Post Thumbnails
- KeePass实现Chrome浏览器自动填充密码方法一
- LNMP一键包nginx 301强制跳转到https教程
- KeePass实现Chrome浏览器自动填充密码方法二
- #建站# 免费的VPS管理软件Xshell8/Xftp8中文版下载
- 使用Xshell 8连接VPS教程_电脑登录vps的方法
- WordPress评论界面添加烟花????效果
- 不同浏览器书签同步方案:坚果云+Floccus_详细使用教程
- iOS端KeePassXC客户端APP:Strongbox Password Safe
- 给WordPress评论中的Gravatar头像图片添加ALT属性


