Is there any last() in YUI NodeList?

Is there any last() in YUI NodeList?

本文关键字:in YUI NodeList there any last Is      更新时间:2023-09-26

假设我尝试选择YUI3中节点列表的最后一个子节点:

var node = Y.one('#node');
node.all('button').last();   // pseudo-code, not actually working!

是否有一个相当于jquery的last()(和first()分别)在YUI3?

我检查了API文档,没有找到任何可比的。

node.one('button:last-child')

也不行

one('button:last-child')

应该工作。请看这个例子。然而,:last-child是指它的父的最后一个子,所以它是高度依赖于你的DOM结构。

<ul>
    <li><button>Foo</button></li>
    <li><button>Bar</button></li>
    <li><button>Baz</button></li>
</ul>
<script>
Y.one('button:last-child'); // yields <button>Foo</button>
// should be written like so
Y.one('li:last-child button');
</script>

我看不到,但这似乎可以完成工作:

var links=Y.all("a"),
lastLink=links.item(links.size()-1)

详情请参阅http://yuilibrary.com/yui/docs/api/classes/NodeList.html#method_item