gpt4 book ai didi

jquery - 如何使用 jQuery 获取 XML 树中的特定节点

转载 作者:数据小太阳 更新时间:2023-10-29 02:07:06 25 4
gpt4 key购买 nike

我有以下 XML 文件:

<?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>

以及以下 jQuery 代码:

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

...

});

如何获取 ID == 1 的单词?

这个 ajax 函数在另一个函数中(getWord() 函数)。我想用任何 ID 获取单词并将该值分配给 getWord() 函数的局部变量。

我该怎么做?

最佳答案

您始终可以求助于编写自定义过滤器。

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

关于jquery - 如何使用 jQuery 获取 XML 树中的特定节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6176845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com