开发者

How to get a specific node in XML tree with jQuery

开发者 https://www.devze.com 2023-03-09 12:36 出处:网络
I have the following XML file: <?xml version=\"1.0\" encoding=\"iso-8859-1\"?> <words> <word>

I have the following XML file:

<?xml version="1.0" encoding="iso-8859-1"?>
<words>
    <word>
        <id>1</id>
        <name>Teacher</name>
    </word>
    <word>
     开发者_运维技巧   <id>2</id>
        <name>Pitcher</name>
    </word>
</words>

And the following jQuery code:

$.ajax({
    type: "GET",
    url: "sites.xml",
    dataType: "xml",
    success: function(xml) {
        $(xml).find('word').each(function(){

        ...

});

How can I get word with ID == 1?

This ajax function is inside another function (getWord() function). I want to get word with any ID and assign that value to a local variable of getWord() function.

How can I do that?


You can always resort to writing a custom filter.

$(xml).find("word").filter(function () {
  return $(this).find("id").text() == "1";
}).each(function () {
  console.log(this);
});


$(xml).find('word').each(function(){

       if(parseInt($(this).find('id').text()) == '1')
{

      // your logic here
}
});
0

精彩评论

暂无评论...
验证码 换一张
取 消