jQuery XML选择 [英] jquery xml select

查看:79
本文介绍了jQuery XML选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何选择其子标签关键字的文本以'001'开头的项目?

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);
});

推荐答案

您需要 .filter() 在这种情况下:

You need .filter() in this case:

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

这将过滤具有以001开头的文本的键元素的项目.如果您完全可以修改架构,这将更快....如果要处理许多项目,则在子级中搜索过滤器总的来说会有点昂贵.

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 的注释建议是一个不错的选择,如果它是一个选项,则该项目具有属性而不是属性内部元素,您可以使用属性开始-选择器,像这样:

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); });

这篇关于jQuery XML选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆