简体   繁体   English

使用纯JavaScript选择带有冒号的XML元素

[英]Select XML element with colon using plain JavaScript

I'm working with the XML data from Apple's RSS Feed Generator and I've come across a small problem. 我正在使用Apple的RSS Feed Generator中的XML数据,但遇到了一个小问题。 If I want to select an element from the XML document, normally I would just use querySelector or getElementsByTagName. 如果要从XML文档中选择一个元素,通常只使用querySelector或getElementsByTagName。 Unfortunately, this doesn't work if I want to select an element that has a colon in it, for example <im:name> . 不幸的是,如果我要选择一个带有冒号的元素(例如<im:name> ,这将无法正常工作。

I've tried various things like escaping the colon with backslashes (this works with JQuery), but nothing seems to work. 我已经尝试过各种方法,例如用反斜杠将冒号转义(这适用于JQuery),但是似乎没有任何效果。

I found I was able to do this by using getElementsByTagName('image') (so omitting the "im:" part of the element tag). 我发现我可以通过使用getElementsByTagName('image')来做到这一点(因此省略了元素标签的“ im:”部分)。 This seems to work in all browsers except Firefox (not sure about Safari, don't have access to that at the moment). 这似乎适用于除Firefox之外的所有浏览器(不确定Safari,目前无法访问)。 In Firefox it seems I have to select it with the namespace included, getElementsByTagName('im:image') . 在Firefox中,似乎必须选择包含名称空间的getElementsByTagName('im:image')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM