jQuery.each({
appendTo: "append",
prependTo: "prepend",
insertBefore: "before",
insertAfter: "after",
replaceAll: "replaceWith"
}, function( name, original ) {
jQuery.fn[ name ] = function( selector ) {
var elems,
i = 0,
ret = [],
insert = jQuery( selector ),
last = insert.length - 1;
for ( ; i <= last; i++ ) {
elems = i === last ? this : this.clone(true);
jQuery( insert[i] )[ original ]( elems );
//现代浏览器调用apply会把jQuery对象当如数组,但是老版本ie需要使用.get()
core_push.apply( ret, elems.get() );
}
return this.pushStack( ret );
};
});
append: function() {
return this.domManip(arguments, true, function( elem ) {
if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
this.appendChild( elem );
}
});
}
var first, node, hasScripts, scripts, doc, fragment, i = 0, l = this.length, set = this, iNoClone = l - 1, value = args[0], isFunction = jQuery.isFunction( value );
//webkit下,我们不能克隆文含有checked的档碎片
if ( isFunction || !( l <= 1 || typeof value !== "string" || jQuery.support.checkClone || !rchecked.test( value ) ) ) {
return this.each(function( index ) {
var self = set.eq( index );
//如果args[0]是函数,则执行函数返回结果替换原来的args[0]
if ( isFunction ) {
args[0] = value.call( this, index, table ? self.html() : undefined );
}
self.domManip( args, table, callback );
});
}
fragment = jQuery.buildFragment( args, this[ 0 ].ownerDocument, false, this );
first = fragment.firstChild;
if ( fragment.childNodes.length === 1 ) {
fragment = first;
}
scripts = jQuery.map( getAll( fragment, "script" ), disableScript ); hasScripts = scripts.length;
for ( ; i < l; i++ ) {
node = fragment;
if ( i !== iNoClone ) {
node = jQuery.clone( node, true, true );
// Keep references to cloned scripts for later restoration
if ( hasScripts ) {
jQuery.merge( scripts, getAll( node, "script" ) );
}
}
callback.call(
table && jQuery.nodeName( this[i], "table" ) ?
findOrAppend( this[i], "tbody" ) :
this[i],
node,
i
);
}
if ( hasScripts ) {
doc = scripts[ scripts.length - 1 ].ownerDocument;
// Reenable scripts
jQuery.map( scripts, restoreScript );
//在第一个文档插入使执行可执行脚本
for ( i = 0; i < hasScripts; i++ ) {
node = scripts[ i ];
if ( rscriptType.test( node.type || "" ) &&
!jQuery._data( node, "globalEval" ) && jQuery.contains( doc, node ) ) {
if ( node.src ) {
// Hope ajax is available...
jQuery.ajax({
url: node.src,
type: "GET",
dataType: "script",
async: false,
global: false,
"throws": true
});
} else {
jQuery.globalEval( ( node.text || node.textContent || node.innerHTML || "" ).replace( rcleanScript, "" ) );
}
}
}
}
jQuery.fn.text: function( value ) {
return jQuery.access( this, function( value ) {
return value === undefined ?
jQuery.text( this ) :
this.empty().append( ( this[0] && this[0].ownerDocument || document ).createTextNode( value ) );
}, null, value, arguments.length );
}
jQuery.fn.html: function( value ) {
return jQuery.access( this, function( value ) {...}, null, value, arguments.length );
}
if ( value === undefined ) {
return elem.nodeType === 1 ?
elem.innerHTML.replace( rinlinejQuery, "" ) :
undefined;
}
//看看我们是否可以走了一条捷径,只需使用的innerHTML
//需要执行的代码script|style|link等不能使用innerHTML
//htmlSerialize:确保link节点能使用innerHTML正确序列化,这就需要在IE浏览器的包装元素
//leadingWhitespace:IE strips使用.innerHTML需要以空白开头
//不是需要额外添加结束标签或外围包装标签的元素
if ( typeof value === "string" && !rnoInnerhtml.test( value ) &&
( jQuery.support.htmlSerialize || !rnoshimcache.test( value ) ) &&
( jQuery.support.leadingWhitespace || !rleadingWhitespace.test( value ) ) &&
!wrapMap[ ( rtagName.exec( value ) || ["", ""] )[1].toLowerCase() ] ) {
value = value.replace( rxhtmlTag, "<$1></$2>" );
try {
for (; i < l; i++ ) {
//移除元素节点和缓存,阻止内存泄漏
elem = this[i] || {};
if ( elem.nodeType === 1 ) {
jQuery.cleanData( getAll( elem, false ) );
elem.innerHTML = value;
}
}
elem = 0;
//如果使用innerHTML抛出异常,使用备用方法
} catch(e) {}
}
//备用方法,使用append添加节点
if ( elem ) {
this.empty().append( value );
}
jQuery.fn.wrapAll(用单个标签将所有匹配元素包裹起来)
处理步骤:
传入参数是函数则将函数结果传入
if ( jQuery.isFunction( html ) ) {
return this.each(function(i) {
jQuery(this).wrapAll( html.call(this, i) );
});
}
创建包裹层
//获得包裹标签 The elements to wrap the target around
var wrap = jQuery( html, this[0].ownerDocument ).eq(0).clone(true);
if ( this[0].parentNode ) {
wrap.insertBefore( this[0] );
}
wrap.map(function() {
var elem = this;
while ( elem.firstChild && elem.firstChild.nodeType === 1 ) {
elem = elem.firstChild;
}
return elem;
}).append( this );
<div id='center' class="center">
<div id='ss' class="center">
<input type='submit' id='left' class="left">
</div>
</div>
<div class="right">我是right</div>
$('#center').wrapAll("<p></p>")后,dom变成了
<p>
<div id="center" class="center">
<div id="ss" class="center">
<input type="submit" id="left" class="left">
</div>
</div>
</p>
<div class="right">我是right</div>
<p>
<div id="center" class="center"></div>
<div id="ss" class="center">
<input type="submit" id="left" class="left">
</div>
<div class="right">我是right</div>
</p>
if ( jQuery.isFunction( html ) ) {
return this.each(function(i) {
jQuery(this).wrapInner( html.call(this, i) );
});
}
return this.each(function() {
var self = jQuery( this ),
contents = self.contents();
if ( contents.length ) {
contents.wrapAll( html );
} else {
self.append( html );
}
});
<div id="center" class="center"> <p> <div id="ss" class="center"> <p> <input type="submit" id="left" class="left"> </p> </div> </p> </div> <div class="right"> <p> 我是right </p> </div>
return this.each(function(i) {
jQuery( this ).wrapAll( isFunction ? html.call(this, i) : html );
});
<p> <div id="center" class="center"> <p> <div id="ss" class="center"> <input type="submit" id="left" class="left"> </div> </p> </div> </p> <p> <div class="right">我是right</div> </p>
return this.parent().each(function() {
if ( !jQuery.nodeName( this, "body" ) ) {
jQuery( this ).replaceWith( this.childNodes );
}
}).end();
执行$('div').wrap()后结果DOM变成
<div id="ss" class="center">
<input type="submit" id="left" class="left">
</div>
<div class="right">我是right</div>
for ( ; (elem = this[i]) != null; i++ ) {
if ( !selector || jQuery.filter( selector, [ elem ] ).length > 0 ) {
// detach传入的参数keepData为true,不删除缓存
if ( !keepData && elem.nodeType === 1 ) {
//清除缓存
jQuery.cleanData( getAll( elem ) );
}
if ( elem.parentNode ) {
if ( keepData && jQuery.contains( elem.ownerDocument, elem ) ) {
setGlobalEval( getAll( elem, "script" ) );
}
elem.parentNode.removeChild( elem );
}
}
}
id = elem[ internalKey ]; data = id && cache[ id ];
if ( data.events ) {
for ( type in data.events ) {
if ( special[ type ] ) {
jQuery.event.remove( elem, type );
//这是一个快捷方式,以避免jQuery.event.remove的开销
} else {
jQuery.removeEvent( elem, type, data.handle );
}
}
}
//当jQuery.event.remove没有移除cache的时候,移除cache
if ( cache[ id ] ) {
delete cache[ id ];
//IE不允许从节点使用delete删除expando特征,
//也能对文件节点使用removeAttribute函数;
//我们必须处理所有这些情况下,
if ( deleteExpando ) {
delete elem[ internalKey ];
} else if ( typeof elem.removeAttribute !== core_strundefined ) {
elem.removeAttribute( internalKey );
} else {
elem[ internalKey ] = null;
}
core_deletedIds.push( id );
}
detach: function( selector ) {
return this.remove( selector, true );
},
for ( ; (elem = this[i]) != null; i++ ) {
//防止内存泄漏移除元素节点缓存
if ( elem.nodeType === 1 ) {
jQuery.cleanData( getAll( elem, false ) );
}
//移除所有子节点
while ( elem.firstChild ) {
elem.removeChild( elem.firstChild );
}
// IE<9,select节点需要将option置空
if ( elem.options && jQuery.nodeName( elem, "select" ) ) {
elem.options.length = 0;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有