append: function() { //传入arguments对象,true为要对表格进行特殊处理,回调函数 returnthis.domManip(arguments, true, function(elem){ if (this.nodeType ==1) this.appendChild( elem ); }); }, domManip: function( args, table, callback ) { if ( this[0] ) {//如果存在元素节点 var fragment = (this[0].ownerDocument ||this[0]).createDocumentFragment(), //注意这里是传入三个参数 scripts = jQuery.clean( args, (this[0].ownerDocument ||this[0]), fragment ), first = fragment.firstChild;
if ( first ) for ( var i =0, l =this.length; i < l; i++ ) callback.call( root(this[i], first), this.length >1|| i >0? fragment.cloneNode(true) : fragment );
if ( scripts ) jQuery.each( scripts, evalScript ); }
// !context.createElement fails in IE with an error but returns typeof 'object' if ( typeof context.createElement ==="undefined" ) //确保context为文档对象 context = context.ownerDocument || context[0] && context[0].ownerDocument || document;
// If a single string is passed in and it's a single tag // just do a createElement and skip the rest //如果文档对象里面只有一个标签,如<div> //我们大概可能是在外面这样调用它$(this).append("<div>") //这时就直接把它里面的元素名取出来,用document.createElement("div")创建后放进数组返回 if ( !fragment && elems.length ===1&&typeof elems[0] ==="string" ) { var match =/^<(\w+)\s*\/?>$/.exec(elems[0]); if ( match ) return [ context.createElement( match[1] ) ]; } //利用一个div的innerHTML创建众节点 var ret = [], scripts = [], div = context.createElement("div"); //如果我们是在外面这样添加$(this).append("<td>表格1</td>","<td>表格1</td>","<td>表格1</td>") //jQuery.each按它的第四种支分方式(没有参数,有length)遍历aguments对象,callback.call( value, i, value ) jQuery.each(elems, function(i, elem){//i为索引,elem为arguments对象里的元素 if ( typeof elem ==="number" ) elem +='';
if ( !elem ) return;
// Convert html string into DOM nodes if ( typeof elem ==="string" ) { // Fix "XHTML"-style tags in all browsers 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 +">"; });
// Trim whitespace, otherwise indexOf won't work as expected var tags = elem.replace(/^\s+/, "").substring(0, 10).toLowerCase();
var wrap = // option or optgroup !tags.indexOf("<opt") && [ 1, "<select multiple='multiple'>", "</select>" ] ||
// Go to html and back, then peel off extra wrappers div.innerHTML = wrap[1] + elem + wrap[2];//比如"<table><tbody><tr>" +<td>表格1</td>+"</tr></tbody></table>" // Move to the right depth while ( wrap[0]-- ) div = div.lastChild;
//处理IE自动插入tbody,如我们使用$('<thead></thead>')创建HTML片断,它应该返回 //'<thead></thead>',而IE会返回'<thead></thead><tbody></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 : [];