jquery xml select

给你一囗甜甜゛ 提交于 2019-12-13 03:43:27

问题


How to select items whose sub-tag key's text starts with '001'?

<root>
    <item>
        <key>001001</key>
        <text>thanks</text>
    </item>
    <item>
        <key>001002</key>
        <text>very</text>
    </item>
    <item>
        <key>002001</key>
        <text>much</text>
    </item>
</root>



$(xml).find("item>[filter string]").each(function()
{
    alert(this);
});

回答1:


You need .filter() in this case:

$(xml).find("item").filter(function() {
  return $(this).find("key").text().indexOf('001') === 0;
}).each(function() {
    alert(this);
});

This filters the items by those having a key element who's text starts with 001. If you could modify the schema at all though, this would be much faster...searching in the children for the filter is a bit expensive overall if you're dealing with many items.

Jake's comment suggestion is a good one if it's an option, if an item had attributes instead of inner elements, you could do it much simpler with the attribute starts-with selector, like this:

$(xml).find("item[key^=001]").each(function() { alert(this); });


来源:https://stackoverflow.com/questions/2672176/jquery-xml-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!