-2

在浏览器中,find("Section") 不工作我有这个 xml 内容

    var xmlContent = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?/><Section type="two_column" name="Section Name" id="attr_id_lines_9810"><Column align="Left"><Label>One</Label><Value>test</Value></Column><Column align="Right"><Label>Two</Label><Value>222</Value></Column></Section>';
$(xmlContent).find("Section").each(function(){console.log($(this).attr('type");});

在 ie 浏览器中,此代码不起作用。但其他浏览器显示正确的结果。如果有人知道解决方案,请更新。

4

1 回答 1

0

XML 声明不是“自动关闭”或配对标记。删除斜线。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?/>

应该:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

使用filter(), not.find()因为“Section”不是子元素。您还有一个不匹配的引号,并且缺少右括号(其中任何一个都会使 IE 发疯)。

$(xmlContent).find("Section").each(function(){console.log($(this).attr('type");});

应该:

$(xmlContent).filter("Section").each(function(){console.log($(this).attr('type'));});

放在一起就是:

var xmlContent = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Section type="two_column" name="Section Name" id="cust_attr_estimate_lines_9810"><Column align="Left"><Label>One</Label><Value>test</Value></Column><Column align="Right"><Label>Two</Label><Value>222</Value></Column></Section>';
$(xmlContent).filter("Section").each(function () {
    console.log($(this).attr('type'));
});

我可以在 IE8(以及 FF 和 Chrome)的开发者工具控制台中运行它,得到预期的结果(two_column)没有任何问题。

于 2013-04-18T15:01:03.793 回答