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

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');


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值