我想在 XML 子元素上使用 jQuery.each() 方法。
下面是我的代码。
$(function() {
var xml = "<approvalcontent><vac_applier>Name</vac_applier><vac_sdate>2017-02-03</vac_sdate><vac_edate>2017-02-10</vac_edate><vac_reason>kind</vac_reason></approvalcontent>";
bindContent(xml);
});
function bindContent(xml) {
$(xml).find("approvalcontent").children().each(function(){
alert("here!");
});
}
但是这个每个函数都显示了任何东西。 我想要屋顶尽可能多的 Xml 元素。 我该如何解决?
最佳答案
jQuery 的 find()
仅适用于后代,您的 <approvalcontent>
element 是一个根元素,所以你想要的可能是 filter()
相反
$(xml).filter("approvalcontent")...
然而,您应该在访问 XML 之前对其进行解析,因为这将为您提供一个实际有效的 XML 文档,您可以使用 find()
function bindContent(xml) {
var parsed = $.parseXML(xml);
$(parsed).find("approvalcontent").children().each(function() {
alert("here!");
});
}
关于javascript - 如何在Xml子元素中选择和每个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42085530/