恩 记录代码中觉得写的比较有意思的代码片段
第一个来自clean:方法
用正则取标签
if ( typeof elem === "string" ) {
elem = elem.replace(/(<(/w+)[^>]*?)//>/g, function(all, front, tag){
return tag.match(/^(abbr|br|col|img|input|link|meta|param|hr|area|embed)$/i) ?
all :
front + "></" + tag + ">";
});
});
如果字符串是按<xxx/>这中格式来的话
replace就会进行 不然不会进行replace
如'<div><span></span></div>'则不进行替换 最后还是返回本身
如果是<div/>则进行替换
function中
all代表 整个字符串
front代表除去结尾部分/>的字符串
tag代表标签名
tag.match(/^(abbr|br|col|img|input|link|meta|param|hr|area|embed)$/i)
可以看到里面所有的但是单标签
所以返回all 就是整个字符串
剩下的双标签的就返回 front + "></" + tag + ">"
为什么不写成"<"+tag++ "></" + tag + ">"
因为有可能标签是带有属性的
如<div style='width:50px'/>
这样 front 就可以匹配到<div style='width:50px'
所以能够把属性也返回了
为了方便写成函数的形式方便调用
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns="http://www.w3.org/1999/xhtml ">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title></title>
</head>
<body >
<div id='ss'>vivivivivivivivivivi</div>
</body>
<script type="text/javascript">
(function(){
var window = this,
jQuery = window.jQuery = window.$ = function( selector, context ) {
return new jQuery.fn.init( selector, context );
},
quickExpr = /^[^<]*(<(.|/s)+>)[^>]*$|^#([/w-]+)$/,
isSimple = /^.[^:#/[/.,]*$/;
//------------------------------------------jq扩展函数开始--------------------------------
jQuery.extend = function() {
var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options;
if ( typeof target === "boolean" ) {
deep = target;
target = arguments[1] || {};
i = 2;
}
if ( typeof target !== "object" && !jQuery.isFunction(target) )
target = {};
if ( length == i ) {
target = this;
--i;
}
for ( ; i < length; i++ )
if ( (options = arguments[ i ]) != null )
for ( var name in options ) {
var src = target[ name ], copy = options[ name ];
if ( target === copy )
continue;
if ( deep && copy && typeof copy === "object" && !copy.nodeType )
target[ name ] = jQuery.extend( deep,
src || ( copy.length != null ? [ ] : { } )
, copy );
else if ( copy !== undefined )
target[ name ] = copy;
}
return target;
};
//------------------------------------------jq扩展函数结束--------------------------------
//------------------------------------------jq类开始--------------------------------
jQuery.fn = jQuery.prototype = {
init : function( selector, context ){
selector = selector || document;
if ( selector.nodeType ) {
this[0] = selector;
this.length = 1;
this.context = selector;
return this;
};
if ( typeof selector === "string" ) {
var match = quickExpr.exec( selector );
if ( match && (match[1] || !context) ) {
if(match[1]){
selector = jQuery.clean( [ match[1] ], context );
}else{
var elem = document.getElementById( match[3] );
//ie6下
/*if ( elem && elem.id != match[3] )
return jQuery().find( selector );*/
var ret = jQuery( elem || [] );
ret.context = document;
ret.selector = selector;
return ret;
};
}
}
return this.setArray(jQuery.isArray( selector )?selector :jQuery.makeArray(selector));
},
setArray: function( elems ) {
this.length = 0;
Array.prototype.push.apply( this, elems );
return this;
}
}
jQuery.fn.init.prototype = jQuery.fn;
//------------------------------------------jq类结束--------------------------------
//------------------------------------------jq方法扩展开始---------------------------------------
var exclude = /z-?index|font-?weight|opacity|zoom|line-?height/i,
defaultView = document.defaultView || {},
toString = Object.prototype.toString;
jQuery.extend({
clean: function( elems, context, fragment ) {
context = context || document;
if ( typeof context.createElement === "undefined" )
context = context.ownerDocument || context[0] && context[0].ownerDocument || document;
var ret = [], scripts = [], div = context.createElement("div");
jQuery.each(elems, function(i, elem){
if ( typeof elem === "number" )
elem += '';
if ( !elem )
return;
if ( typeof elem === "string" ) {
elem = elem.replace(/(<(/w+)[^>]*?)//>/g, function(all, front, tag){
return tag.match(/^(abbr|br|col|img|input|link|meta|param|hr|area|embed)$/i) ?
all :
front + "></" + tag + ">";
});
var tags = elem.replace(/^/s+/, "").substring(0, 10).toLowerCase();
var wrap =
!tags.indexOf("<opt") &&
[ 1, "<select multiple='multiple'>", "</select>" ] ||
!tags.indexOf("<leg") &&
[ 1, "<fieldset>", "</fieldset>" ] ||
tags.match(/^<(thead|tbody|tfoot|colg|cap)/) &&
[ 1, "<table>", "</table>" ] ||
!tags.indexOf("<tr") &&
[ 2, "<table><tbody>", "</tbody></table>" ] ||
// <thead> matched above
(!tags.indexOf("<td") || !tags.indexOf("<th")) &&
[ 3, "<table><tbody><tr>", "</tr></tbody></table>" ] ||
!tags.indexOf("<col") &&
[ 2, "<table><tbody></tbody><colgroup>", "</colgroup></table>" ] ||
!jQuery.support.htmlSerialize && //!jQuery.support.htmlSerialize表示在ie浏览器下
[ 1, "div<div>", "</div>" ] ||
[ 0, "", "" ];
div.innerHTML = wrap[1] + elem + wrap[2];
while ( wrap[0]-- )
div = div.lastChild; //找到正确的父元素
//在ie6下 会执行下面的代码
//去掉table中的 tbody标签
if ( !jQuery.support.tbody ) {
// String was a <table>, *may* have spurious <tbody>
var hasBody = /<tbody/i.test(elem),
tbody = !tags.indexOf("<table") && !hasBody ?
div.firstChild && div.firstChild.childNodes :
// String was a bare <thead> or <tfoot>
wrap[1] == "<table>" && !hasBody ?
div.childNodes :
[];
for ( var j = tbody.length - 1; j >= 0 ; --j )
if ( jQuery.nodeName( tbody[ j ], "tbody" ) && !tbody[ j ].childNodes.length )
tbody[ j ].parentNode.removeChild( tbody[ j ] );
}
if ( !jQuery.support.leadingWhitespace && /^/s/.test( elem ) )
div.insertBefore( context.createTextNode( elem.match(/^/s*/)[0] ), div.firstChild );
elem = jQuery.makeArray( div.childNodes );
}
if ( elem.nodeType )
ret.push( elem );
else
ret = jQuery.merge( ret, elem );
});
return ret;
},
each: function( object, callback, args ) {
var name, i = 0, length = object.length;
if ( args ) {
if ( length === undefined ) {
for ( name in object )
if ( callback.apply( object[ name ], args ) === false )
break;
} else
for ( ; i < length; )
if ( callback.apply( object[ i++ ], args ) === false )
break;
} else {
if ( length === undefined ) {
for ( name in object )
if ( callback.call( object[ name ], name, object[ name ] ) === false )
break;
} else
for ( var value = object[0];
i < length && callback.call( value, i, value ) !== false; value = object[++i] ){}
}
return object;
},
makeArray: function( array ) {
var ret = [];
if( array != null ){
var i = array.length;
if( i == null || typeof array === "string" || jQuery.isFunction(array) || array.setInterval )
ret[0] = array;
else
while( i )
ret[--i] = array[i];
}
return ret;
},
isFunction: function( obj ) {
return toString.call(obj) === "[object Function]";
},
isArray: function(obj){
return toString.call(obj) === "[object Array]";
},
merge: function( first, second ) {
var i = 0, elem, pos = first.length;
if ( !jQuery.support.getAll ) {
while ( (elem = second[ i++ ]) != null )
if ( elem.nodeType != 8 )
first[ pos++ ] = elem;
} else
while ( (elem = second[ i++ ]) != null )
first[ pos++ ] = elem;
return first;
},
});
//------------------------------------------jq方法扩展结束---------------------------------------
(function(){
jQuery.support = {};
var div = document.createElement("div");
div.style.display = "none";
div.innerHTML = ' <link/><table></table><a href="/a" style="color:red;float:left;opacity:.5;">a</a><select><option>text</option></select><object><param/></object>';
var all = div.getElementsByTagName("*"),
a = div.getElementsByTagName("a")[0];
if ( !all || !all.length || !a ) {
return;
}
jQuery.support = {
// IE strips leading whitespace when .innerHTML is used
leadingWhitespace: div.firstChild.nodeType == 3,
// Make sure that tbody elements aren't automatically inserted
// IE will insert them into empty tables
tbody: !div.getElementsByTagName("tbody").length,
// Make sure that you can get all elements in an <object> element
// IE 7 always returns no results
objectAll: !!div.getElementsByTagName("object")[0]
.getElementsByTagName("*").length,
// Make sure that link elements get serialized correctly by innerHTML
// This requires a wrapper element in IE
htmlSerialize: !!div.getElementsByTagName("link").length,
// Get the style information from getAttribute
// (IE uses .cssText insted)
style: /red/.test( a.getAttribute("style") ),
// Make sure that URLs aren't manipulated
// (IE normalizes it by default)
hrefNormalized: a.getAttribute("href") === "/a",
// Make sure that element opacity exists
// (IE uses filter instead)
opacity: a.style.opacity === "0.5",
// Verify style float existence
// (IE uses styleFloat instead of cssFloat)
cssFloat: !!a.style.cssFloat,
// Will be defined later
scriptEval: false,
noCloneEvent: true,
boxModel: null
};
})();
})();
alert($('#ss')[0].innerHTML);
</script>
</html>