您的位置:首页 > Web前端 > JQuery

jquery对象的基本方法和属性--学习笔记

2017-10-01 09:47 776 查看
toArray:专属组

toArray: function() {
return core_slice.call( this );
},$('div') : { 0 : div , 1 : div , 2 : div , length : 3 },得到的是json对象;
$('div').toArray() : [div,div,div],得到的是数组。原生数组,不能调用jQuery方法。

get:转原生集合

get: function( num ) {
return num == null ?

// 没有参数时,调用toArray方法
this.toArray() :

// 如果是负数,要加上数组长度
( num < 0 ? this[ this.length + num ] : this[ num ] );
},
$('div').get(-1).innerHTML = '222222222';将最后一个div的内容变为222222222

pushStack:jQuery对象的入栈

pushStack: function( elems ) {

// Build a new jQuery matched element set
var ret = jQuery.merge( this.constructor(), elems );

// Add the old object onto the stack (as a reference)
ret.prevObject = this;
ret.context = this.context;

// Return the newly-formed element set
return ret;
},


调用pushStack方法

slice: function() {
return this.pushStack( core_slice.apply( this, arguments ) );
},

first: function() {
return this.eq( 0 );
},

last: function() {
return this.eq( -1 );
},

eq: function( i ) {
var len = this.length,
j = +i + ( i < 0 ? len : 0 );
return this.pushStack( j >= 0 && j < len ? [ this[j] ] : [] );
},

map: function( callback ) {
return this.pushStack( jQuery.map(this, function( elem, i ) {
return callback.call( elem, i, elem );
}));
},

end: function() {
return this.prevObject || this.constructor(null);
},

<div>div</div>
<div>div</div>
<div>div</div>
<div>div</div>
<span>span</span>


$('div').pushStack( $('span') ).css('background','red').end().css('background','yellow');



$('div').slice(1,3).css('background','red').end().css('color','blue');



$('div').eq(0).css('background','red');

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐