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
}
});
精彩评论