簡體   English   中英

原型相當於jQuery eq方法

[英]Prototype equivalent of jQuery eq method

給出以下html代碼:

<ul id="fun">
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
    <li>Four</li>
    <li>Five</li>
</ul>

我可以這樣選擇jquery中的第三個li:

$('#fun li').eq(2);

在原型中我知道我可以做類似的事情:

$("fun").select('li').each(function(i,v){
    if(v == 2){/*do whatever*/}
});

所以我要問的是,原型是否具有像jquerys eq這樣的方法,該方法允許您在元素數組中選擇一個特別索引的元素?

謝謝

以下是其他一些方法

$('fun').down(2);
//for the 2nd child of #fun no matter what element

要么

$('fun').down('li',2)
//for the 2nd child <li> element

並且您不需要將結果包裝在另一個$()因為元素結果已經擴展,而down()方法只返回一個元素,因此您不需要添加數組索引[2]

http://api.prototypejs.org/dom/Element/prototype/down/

好像你們忘記了$$

$$('#fun li')[2]

小提琴

$('#fun li').eq(2);

相當於:

$($('#fun li')[2]);

因此,您可以使用本機JavaScript來完成此操作。

$($("fun").select('li')[2]).whatever

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM