jquery 學習之一 對象訪問

來源:互聯網
上載者:User

each()

each(callback)

以每一個匹配的元素作為上下文來執行一個函數。意味著,每次執行傳遞進來的函數時,函數中的this關鍵字都指向一個不同的DOM元素(每次都是一個不同的匹配元素)。

而且,在每次執行函數時,都會給函數傳遞一個表示作為執行環境的元素在匹配的元素集合中所處位置的數字值作為參數(從零開始的整形)。

返回 'false' 將停止迴圈 (就像在普通的迴圈中使用 'break')。返回 'true' 跳至下一個迴圈(就像在普通的迴圈中使用'continue')。

Execute a function within the context of every matched element.This means that every time the passed-in function is executed (which is once for every element matched) the 'this' keyword points to the specific DOM element.

Additionally, the function, when executed, is passed a single argument representing the position of the element in the matched set (integer, zero-index).

Returning 'false' from within the each function completely stops the loop through all of the elements (this is like using a 'break' with a normal loop). Returning 'true' from within the loop skips to the next iteration (this is like using a 'continue' with a normal loop).

傳回值

jQuery

參數

callback (Function) : 對於每個匹配的元素所要執行的函數

樣本

迭代兩個映像,並設定它們的 src 屬性。注意:此處 this 指代的是 DOM 對象而非 jQuery 對象。

HTML 程式碼:

<img/><img/>

jQuery 代碼:

$("img").each(function(i){
this.src = "test" + i + ".jpg";
});

結果:

[ <img src="test0.jpg" />, <img src="test1.jpg" /> ]

如果你想得到 jQuery對象,可以使用 $(this) 函數。

jQuery 代碼:

$("img").each(function(){
$(this).toggleClass("example");
});

你可以使用 'return' 來提前跳出 each() 迴圈。

HTML 程式碼:

<button>Change colors</button><span></span> <div></div> <div></div><div></div> <div></div><div id="stop">Stop here</div> <div></div><div></div><div></div>

jQuery 代碼:

$("button").click(function () { $("div").each(function (index, domEle) {  // domEle == this  $(domEle).css("backgroundColor", "yellow");  if ($(this).is("#stop")) {  $("span").text("Stopped at div index #" + index);  return false;  } });});

--------------------------------------------------------------------------------------------------------------------------------

size()jQuery 對象中元素的個數。這個函數的傳回值與 jQuery 對象的'length' 屬性一致。The number of elements in the jQuery object.This returns the same number as the 'length' property of the jQuery object.傳回值

Number

樣本

計算文檔中所有圖片數量

HTML 程式碼:

<img src="test1.jpg"/> <img src="test2.jpg"/>

jQuery 代碼:

$("img").size();

結果:

2 -------------------------------------------------------------------------------------------------------------------------lengthjQuery 對象中元素的個數。當前匹配的元素個數。 size 將返回相同的值。The number of elements in the jQuery object.The number of elements currently matched. The size function will return the same value.傳回值

Number

樣本

計算文檔中所有圖片數量

HTML 程式碼:

<img src="test1.jpg"/> <img src="test2.jpg"/>

jQuery 代碼:

$("img").length;

結果:

2 ---------------------------------------------------------------------------------------------------------------------------------------get()

取得所有匹配的 DOM 元素集合。

這是取得所有匹配元素的一種向後相容的方式(不同於jQuery對象,而實際上是元素數組)。

如果你想要直接操作 DOM 對象而不是 jQuery 對象,這個函數非常有用。

Access all matched DOM elements.This serves as a backwards-compatible way of accessing all matched elements (other than the jQuery object itself, which is, in fact, an array of elements). It is useful if you need to operate on the DOM elements themselves instead of using built-in jQuery functions.傳回值

Array<Element>

樣本

選擇文檔中所有映像作為元素數組,並用數組內建的 reverse 方法將數組反向。

HTML 程式碼:

<img src="test1.jpg"/> <img src="test2.jpg"/>

jQuery 代碼:

$("img").get().reverse();

結果:

[ <img src="test2.jpg"/> <img src="test1.jpg"/> ] ---------------------------------------------------------------------------------------------------------------------------------------get(index)取得其中一個匹配的元素。 num表示取得第幾個匹配的元素。這能夠讓你選擇一個實際的DOM 元素並且對他直接操作,而不是通過 jQuery 函數。$(this).get(0)與$(this)[0]等價。Access a single matched DOM element at a specified index in the matched set.This allows you to extract the actual DOM element and operate on it directly without necessarily using jQuery functionality on it. This function called as $(this).get(0) is the equivalent of using square bracket notation on the jQuery object itself like $(this)[0].傳回值

Element

參數

index (Number) :取得第 index 個位置上的元素

樣本

HTML 程式碼:

<img src="test1.jpg"/> <img src="test2.jpg"/>

jQuery 代碼:

$("img").get(0);

結果:

[ <img src="test1.jpg"/> ] ---------------------------------------------------------------------------------------------------------------------------------------index(subject)搜尋與參數表示的對象匹配的元素,並返回相應元素的索引值值。如果找到了匹配的元素,從0開始返回;如果沒有找到匹配的元素,返回-1。Searches every matched element for the object and returns the index of the element, if found, starting with zero.Returns -1 if the object wasn't found.傳回值

Number

參數

subject (Element) : 要搜尋的對象

樣本

返回ID值為foobar的元素的索引值值。

HTML 程式碼:

<div id="foobar"><b></b><span id="foo"></span></div>

jQuery 代碼:

$("*").index($('#foobar')[0])

結果:

5

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.