get
- 參數:
- 做用:
- 將jQ對象轉換爲DOM對象,並獲取該DOM對象
- 說明:
- index爲空、null,undfined,返回全部選中DOM對象
- index爲正整數,返回從左至右索引index的DOM對象
- idnex爲負整數,返回從右至左索引index的DOM對象
- code:
<div class="demo demo1"></div>
<div class="demo demo2"></div>
<div class="demo demo3"></div>
複製代碼
var $Demo = $('.demo');
var firstDemo =$Demo.get(0);
console.log(firstDemo);
var allDemo = $Demo.get(null);
console.log(allDemo);
複製代碼
eq
- 參數:
- 做用:
- 獲取索引值對應的jQ對象
- 說明:
- 當參數爲null, false時,獲取索引爲0的jQ對象
- index爲正整數,返回從左至右索引index的jQ對象
- idnex爲負整數,返回從右至左索引index的jQ對象
- code:
<div class="demo demo1"></div>
<div class="demo demo2"></div>
<div class="demo demo3"></div>
複製代碼
var $Demo = $('.demo');
var firstDemo =$Demo.eq(0);
console.log(firstDemo);
var allDemo = $Demo.eq(null);
console.log(allDemo);
複製代碼
find
- 參數:
- 做用:
- 在已有值的基礎上,獲取符合selector條件的jQ對象
- code:
<div class="wrapper">
<div class="demo demo1"></div>
<span class="demo demo2"></span>
<p class="demo demo3"></p>
</div>
複製代碼
var $Wrapper = $('.wrapper');
var $PDemo = $Wrapper.find('p.demo');
console.log($PDemo);
複製代碼
實現原理
get:
jQuery.prototype.myGet = function (num) {
return num == null ?
Array.prototype.slice.call(this) :
(num >= 0 ? this[num] : this[num + this.length]);
};
複製代碼
eq:
jQuery.prototype.pushStack = function (dom) {
dom.prevObject = this;
return dom;
};
jQuery.prototype.myEq = function (num) {
var dom == null ?
null : (num >= 0 ? this[num] : this[num + this.length]);
return this.pushStack(jQuery(dom));
};
複製代碼