这是一次失败的JQ源码分析

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>

domManip//对封装的节点操作做了参数上的校正支持,与对应处理的调用

appendChild()

通过把一个节点增加到当前节点的 childNodes[]组,给文档树增加节点。

cloneNode()

复制当前节点,或者复制当前节点以及它的所有子孙节点。

hasChildNodes()

如果当前节点拥有子节点,则将返回true。

insertBefore()

给文档树插入一个节点,位置在当前节点的指定子节点之前。如果该节点已经存在,则删除之再插入到它的位置。

removeChild()

从文档树中删除并返回指定的子节点。

replaceChild()

从文档树中删除并返回指定的子节点,用另一个节点替换它。
以上接口都有一个特性,传入的是一个节点元素

function manipulationTarget( elem, content ) {
if ( nodeName( elem, "table" ) &&
nodeName( content.nodeType !== 11 ? content : content.firstChild, "tr" ) ) {
return jQuery( ">tbody", elem )[ 0 ] || elem;
}
return elem;
}


// 5902 节点操作
jQuery.fn.extend( {
detach: function( selector ) { //从 DOM 中移除匹配元素集合。不删除绑定的事件
return remove( this, selector, true );
},

remove: function( selector ) {//移除所有匹配的元素。删除绑定的事件
return remove( this, selector );
},

text: function( value ) { //设置或返回匹配元素的内容。
return access( this, function( value ) {
return value === undefined ?
jQuery.text( this ) :
this.empty().each( function() {
if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
this.textContent = value;
}
} );
}, null, value, arguments.length );
},

append: function() { // 向匹配元素集合中的每个元素结尾插入由参数指定的内容。
return domManip( this, arguments, function( elem ) {
if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
var target = manipulationTarget( this, elem );
target.appendChild( elem );
}
} );
},

prepend: function() { //向匹配元素集合中的每个元素开头插入由参数指定的内容。
return domManip( this, arguments, function( elem ) {
if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) {
var target = manipulationTarget( this, elem );
target.insertBefore( elem, target.firstChild );
}
} );
},

before: function() {//在每个匹配的元素之前插入内容
return domManip( this, arguments, function( elem ) {
if ( this.parentNode ) {
this.parentNode.insertBefore( elem, this );
}
} );
},

after: function() {//在匹配的元素之后插入内容。
return domManip( this, arguments, function( elem ) {
if ( this.parentNode ) {
this.parentNode.insertBefore( elem, this.nextSibling );
}
} );
},

empty: function() { //删除匹配的元素集合中所有的子节点。
var elem,
i = 0;
for ( ; ( elem = this[ i ] ) != null; i++ ) {
if ( elem.nodeType === 1 ) {
// Prevent memory leaks
jQuery.cleanData( getAll( elem, false ) );

// Remove any remaining nodes
elem.textContent = "";
}
}
return this;
},

clone: function( dataAndEvents, deepDataAndEvents ) { //创建匹配元素集合的副本。
dataAndEvents = dataAndEvents == null ? false : dataAndEvents;
deepDataAndEvents = deepDataAndEvents == null ? dataAndEvents : deepDataAndEvents;
return this.map( function() {
return jQuery.clone( this, dataAndEvents, deepDataAndEvents );
} );
},

html: function( value ) { //设置或返回匹配的元素集合中的 HTML 内容。
return access( this, function( value ) {
var elem = this[ 0 ] || {},
i = 0,
l = this.length;

if ( value === undefined && elem.nodeType === 1 ) {
return elem.innerHTML;
}

// See if we can take a shortcut and just use innerHTML
if ( typeof value === "string" && !rnoInnerhtml.test( value ) &&
!wrapMap[ ( rtagName.exec( value ) || [ "", "" ] )[ 1 ].toLowerCase() ] ) {

value = jQuery.htmlPrefilter( value );

try {
for ( ; i < l; i++ ) {
elem = this[ i ] || {};

// Remove element nodes and prevent memory leaks
if ( elem.nodeType === 1 ) {
jQuery.cleanData( getAll( elem, false ) );
elem.innerHTML = value;
}
}

elem = 0;

// If using innerHTML throws an exception, use the fallback method
} catch ( e ) {}
}

if ( elem ) {
this.empty().append( value );
}
}, null, value, arguments.length );
},

replaceWith: function() { //用新内容替换匹配的元素。
var ignored = [];

// Make the changes, replacing each non-ignored context element with the new content
return domManip( this, arguments, function( elem ) {
var parent = this.parentNode;

if ( jQuery.inArray( this, ignored ) < 0 ) {
jQuery.cleanData( getAll( this ) );
if ( parent ) {
parent.replaceChild( elem, this );
}
}

// Force callback invocation
}, ignored );
}
} );
</script>
</body>
</html>

posted @ 2017-06-12 18:31  Charles王志会  阅读(189)  评论(0)    收藏  举报