时间:2016-02-17 21:53 来源: 我爱IT技术网 作者:佚名
欢迎您访问我爱IT技术网,今天小编为你分享的javascript教程:【jQuery.clean使用方法及思路分析】,下面是详细的讲解!
jQuery.clean使用方法及思路分析
jQuery.extend({
clean: function( elems, context, fragment, scripts ) {
// 声明变量
var i, j, elem, tag, wrap, depth, div, hasBody, tbody, len, handleScript, jsTags,
safe=context===document && safeFragment,
ret=[];
// 确保变量context为文档根节点document
if ( !context || typeof context.createDocumentFragment==="undefined" ) {
context=document;
}
// Use the already-created safe fragment if context permits
for ( i=0; (elem=elems[i]) !=null; i++ ) {
// 如果elem为数字,则将其转换为字符串
if ( typeof elem==="number" ) {
elem +="";
}
// 如果elem为undefined,跳出本次循环
if ( !elem ) {
continue;
}
// Convert html string into DOM nodes
// 转换数组项(字符串)为DOM节点
if ( typeof elem==="string" ) {
// 如果不存在html实体编号或标签,则创建文本节点
if ( !rhtml.test( elem ) ) {
elem=context.createTextNode( elem );
}
// 处理是html标签字符串的数组项
else {
// Ensure a safe container in which to render the html
// safe为#document-fragment类型,在ie9以下浏览器中,safe为HTMLDocument类型节点,且nodeNames数组为空
safe=safe || createSafeFragment( context );
// 创建一个div元素并将其插入到文档碎片中
div=context.createElement("div");
safe.appendChild( div );
// Fix "XHTML"-style tags in all browsers
// 除了area,br,col,embed,hr,img,input,link,meta,param这些标签外,
// 将开始标签末尾加入斜杠的标签转换为开始和结束标签
elem=elem.replace(rxhtmlTag, "<$1></$2>");
// Go to html and back, then peel off extra wrappers
// 获取左边第一个标签元素
tag=( rtagName.exec( elem ) || ["", ""] )[1].toLowerCase();
// 获取最外层元素的包裹元素,并将元素包裹在其中
wrap=wrapMap[ tag ] || wrapMap._default;
depth=wrap[0];
div.innerHTML=wrap[1] + elem + wrap[2];
// Move to the right depth
// 如果元素的包裹深度大于1,div重新赋值为元素最近的包裹元素(即:包含第一层包裹元素)
while ( depth-- ) {
div=div.lastChild;
}
// Remove IE's autoinserted <tbody> from table fragments
// 在IE6,7中,清除字符串中空table标签中自动加入的tbody标签(手动加入的除外)
if ( !jQuery.support.tbody ) {
// String was a <table>, *may* have spurious(伪造的) <tbody>
// 判断字符串中是否拥有空tbody标签
hasBody=rtbody.test(elem);
// 如果最外层标签为table且table中没有手动加入tbody
// 变量tbody为div.firstChild.childNodes(自动加入的tbody标签集合)
tbody=tag==="table" && !hasBody ?
div.firstChild && div.firstChild.childNodes :
// String was a bare <thead> or <tfoot>
// 如果字符串中仅有一个空thead或tfoot标签
// 变量tbody为div.childNodes(字符串中的thead和tfoot标签集合)
wrap[1]==="<table>" && !hasBody ?
div.childNodes :
[];
for ( j=tbody.length - 1; j >=0 ; --j ) {
// 排除thead或tfoot标签
if ( jQuery.nodeName( tbody[ j ], "tbody" ) && !tbody[ j ].childNodes.length ) {
// 清除空table标签中自动加入的tbody
tbody[ j ].parentNode.removeChild( tbody[ j ] );
}
}
}
// IE completely kills leading whitespace when innerHTML is used
// 在ie9以下浏览器中,字符串以空白字符串开头,将空白字符串作为div元素的第一个文本子节点
if ( !jQuery.support.leadingWhitespace && rleadingWhitespace.test( elem ) ) {
div.insertBefore( context.createTextNode( rleadingWhitespace.exec(elem)[0] ), div.firstChild );
}
// 获取已经处理完毕的div子节点集合(nodeList对象)
elem=div.childNodes;
// Take out of fragment container (we need a fresh div each time)
// 在下一次循环处理字符串数组项前,清除处理创建过的div元素
div.parentNode.removeChild( div );
}
}
// 如果elem为DOM节点(文本节点)
if ( elem.nodeType ) {
ret.push( elem );
}
// 将nodeList对象中节点合并到返回的数组中
else {
jQuery.merge( ret, elem );
}
}
// Fix #11356: Clear elements from safeFragment
if ( div ) {
elem=div=safe=null;
}
// Reset defaultChecked for any radios and checkboxes
// about to be appended to the DOM in IE 6/7 (#8060)
// 在ie6,7中,拥有checked属性的单选按钮,复选框在插入到其他标签后,选中状态会失效(下面代码修复该bug)
if ( !jQuery.support.appendChecked ) {
for ( i=0; (elem=ret[i]) !=null; i++ ) {
if ( jQuery.nodeName( elem, "input" ) ) {
fixDefaultChecked( elem );
} else if ( typeof elem.getElementsByTagName !=="undefined" ) {
jQuery.grep( elem.getElementsByTagName("input"), fixDefaultChecked );
}
}
}
// Append elements to a provided document fragment
// 将ret数组中的各DOM节点插入到提供的文档碎片中
// 提取dom节点中的script节点,并添加到ret数组中,位置为其原父元素索引位置后
if ( fragment ) {
// Special handling of each script element
handleScript=function( elem ) {
// Check if we consider it executable
// 如果elem元素不存在type属性或者type值为javascript或者为ecmascript
if ( !elem.type || rscriptType.test( elem.type ) ) {
// Detach the script and store it in the scripts array (if provided) or the fragment
// Return truthy to indicate that it has been handled
return scripts ?
scripts.push( elem.parentNode ? elem.parentNode.removeChild( elem ) : elem ) :
fragment.appendChild( elem );
}
};
for ( i=0; (elem=ret[i]) !=null; i++ ) {
// Check if we're done after handling an executable script
if ( !( jQuery.nodeName( elem, "script" ) && handleScript( elem ) ) ) {
// Append to fragment and handle embedded scripts
// 将elem元素添加到文档碎片中并处理嵌入的脚本(script标签元素)
fragment.appendChild( elem );
if ( typeof elem.getElementsByTagName !=="undefined" ) {
// handleScript alters the DOM, so use jQuery.merge to ensure snapshot iteration
jsTags=jQuery.grep( jQuery.merge( [], elem.getElementsByTagName("script") ), handleScript );
// Splice the scripts into ret after their former ancestor and advance our index beyond them
// 将script标签添加到数组,位置为其原父元素索引位置后
ret.splice.apply( ret, [i + 1, 0].concat( jsTags ) );
i +=jsTags.length;
}
}
}
}
return ret;
}
});
关于jQuery.clean使用方法及思路分析的用户互动如下:
相关问题:jquery图片轮播思路
答:首先纠正个错误,开始就隐藏img不要用js去做。js操作dom影响速度,第二是需要在js加载完了才会把图片隐藏,这会导致没加载完前你的图片全部是显示的。 用css来隐藏img,另外图片宽高都写在img里减小渲染框架时间。你们老师这样写例子 误人子弟啊... >>详细
相关问题:==请教各位,xss_clean把提交内容的<script......
答:数据序列化后再提交,jquery中有相关的函数,可以直接用的,你是用的jquery吗? >>详细
相关问题:百度经验中用到的jquery
答:你有jquery的基础就好说一些,没有基础跟你说也不是一言两语说的清楚的。 给你说个思路。在百度中他是有个判断的, 看到list-item-7,list-item-6这些标签没有,他就是根据这些标签来判断现在所在的位置。首先要获取滚动条的scroll-top的值,然后... >>详细
- 【firefox】firefox浏览器不支持innerText的解决
- 【Extjs】Extjs学习过程中新手容易碰到的低级错误
- 【clearInterval】js clearInterval()方法的定义
- 【ComboBox】ComboBox 和 DateField 在IE下消失的
- 【dom】javascript dom追加内容实现示例-追加内容
- 【has】基于jquery的has()方法以及与find()方法以
- 【extjs】Extjs入门之动态加载树代码-动态加载树
- 【checkbox】让checkbox不选中即将选中的checkbox
- 【angularjs_scope】AngularJS中监视Scope变量以
- 【Array】js中更短的 Array 类型转换-类型转换
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
