这是我的代码:
var CDs=$(xml).find("CDs");
var NthChild = $(CDs).find("CD:nth-child(index)"); // nth-child 0th element starts at 1
我在这里使用find方法;我必须指定子节点名称,记为“CD”。是否有一种更快或更短的方法,也许不需要我指定子节点的名称(即“CD”)来查找第 n 个子节点?
谢谢。
更新
我发现了一种不需要按名称指定子节点的格式:
var NthChild = $(CDs).children(":nth-child(index)");
最佳答案
$(xml).find("CDs CD").eq(index);
Because :eq() is a jQuery extension and not part of the CSS specification, queries using :eq() cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. For better performance in modern browsers, use $("your-pure-css-selector").eq(index) instead.
关于javascript - 寻找更快或更好的方法来使用 jquery 选择 XML 节点的第 n 个子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6944122/