jQuery 遍曆 - eq() 方法

來源:互聯網
上載者:User

標籤:item 1   遍曆   負數   round   屬性   highlight   引用   length   asc   

<!DOCTYPE html><html><head>  <style>  div { width:60px; height:60px; margin:10px; float:left;        border:2px solid blue; }  .blue { background:blue; }  </style>  <script type="text/javascript" src="/jquery/jquery.js"></script></head><body><ul>  <li>list item 1</li>  <li>list item 2</li>  <li>list item 3</li>  <li>list item 4</li>  <li>list item 5</li></ul><script>$(‘li‘).eq(2).css(‘background-color‘, ‘red‘);</script></body></html>

 

定義和用法

eq(index) 方法將匹配元素集縮減值指定 index 上的一個。

index:整數,指示元素的位置(最小為 0)。如果是負數,則從集合中的最後一個元素往回計數。

上面的例子調用的結果是為項目 3 設定了紅色背景。請注意,index 是基於零的,並且是在 jQuery 對象中引用元素的位置,而不是在 DOM 樹中

$(‘li‘).eq(2).css(‘background-color‘, ‘red‘);

 

如果提供負數,則指示從集合結尾開始的位置,而不是從開頭開始。

$(‘li‘).eq(-2).css(‘background-color‘, ‘red‘);

 

如果無法根據指定的 index 參數找到元素,則該方法構造帶有空集的 jQuery 對象,length 屬性為 0

$(‘li‘).eq(5).css(‘background-color‘, ‘red‘);

 



jQuery 遍曆 - eq() 方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.