如何使用 Javascript 查找命名空间 HTML 元素
我正在写一些学术性的东西,其中有命名空间的 HTML 元素,例如:
<ns:LinkList id="sitesuteis" cssClass="classone">
<ns:LinkItem id="LI1" href="http://www.ibt.pt/" target="_blank">IBT</ns:LinkItem>
<ns:LinkItem id="LI2" href="http://http://html5demos.com/t/" target="_blank">HTML5 Demos</ns:LinkItem>
<ns:LinkItem id="LI3" href="http://diveintohtml5.ep.io/" target="_blank">Dive into HTML5</ns:LinkItem>
<ns:LinkItem id="LI4" href="http://html5boilerplate.com/" target="_blank">HTML5 Boilerplate</ns:LinkItem>
</ns:LinkList>
现在,在 JavaScript 中,我正在尝试:
var elements = document.getElementsByTagName('ns:LinkItem');
element = elements[0];
console.log(element.getAttribute('id'));
//I get a correct value in all browsers
获取我的 elements[0]
中的所有 ChildNode。它在所有浏览器中工作正常,除了 -IE lt 9-
我尝试过:
var children = element.getElementsByTagName('ns:LinkItem');
console.log(children.length);
和:
var children = Array();
for (i=0; i<element.childNodes.length; i++){
alert(element.childNodes[i].nodeName);
if (element.childNodes[i].nodeName=="NS:LINKITEM"){
children.push(element.childNodes[i]);
}
}
console.log(children.length);
在两个 console.logs 中,我在每个浏览器中都得到了正确的长度(4),除了 Internet Explorer 8 或更少,我得到 0。
根据@Shadow向导,在 Internet Explorer 8 及更低版本中,元素的 canHaveChildren
属性为 false,这意味着死胡同,浏览器根本不支持此标记的子节点,与 < 相同;br例如,/>
不能有子节点。我试过了,确实如此。如果我尝试:
element.parentNode
在 Internet Explorer 8 或更低版本中,我会得到包含我的标记的 div
,而在其他浏览器中,我会得到我的父级
。
我真的需要一个黑客来解决这个问题,但我似乎找不到。
I am writing something academic where I have namespaced HTML elements like:
<ns:LinkList id="sitesuteis" cssClass="classone">
<ns:LinkItem id="LI1" href="http://www.ibt.pt/" target="_blank">IBT</ns:LinkItem>
<ns:LinkItem id="LI2" href="http://http://html5demos.com/t/" target="_blank">HTML5 Demos</ns:LinkItem>
<ns:LinkItem id="LI3" href="http://diveintohtml5.ep.io/" target="_blank">Dive into HTML5</ns:LinkItem>
<ns:LinkItem id="LI4" href="http://html5boilerplate.com/" target="_blank">HTML5 Boilerplate</ns:LinkItem>
</ns:LinkList>
Now, in JavaScript I am trying:
var elements = document.getElementsByTagName('ns:LinkItem');
element = elements[0];
console.log(element.getAttribute('id'));
//I get a correct value in all browsers
to get all the ChildNodes in my elements[0]
. It works fine in all browsers, except -IE lt 9-
I tried:
var children = element.getElementsByTagName('ns:LinkItem');
console.log(children.length);
and:
var children = Array();
for (i=0; i<element.childNodes.length; i++){
alert(element.childNodes[i].nodeName);
if (element.childNodes[i].nodeName=="NS:LINKITEM"){
children.push(element.childNodes[i]);
}
}
console.log(children.length);
In both console.logs, I get the correct length (4) in every browser except Internet Explorer 8 or less where I get 0.
According to @Shadow Wizard, in Internet Explorer 8 and below, the canHaveChildren
property of the element is false which means dead end, the browser simply doesn't support having child nodes for this tag, same way that <br />
can't have child nodes for example. I have tried it and it is true. If I try:
element.parentNode
in Internet Explorer 8 or less, I get the div
that contains my markup and in the other browsers I get my parent <ns:LinkList>
.
I really need a hack for this, and I can't seem to find one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在非 Internet Explorer 浏览器中,我建议使用
getElementsByTagNameNS
来获取特定命名空间中的元素。在 Internet Explorer 中,您可以使用 XPath。
jQuery 还提供了一种使用命名空间的方法;它似乎包含在“jQuery XML 解析与命名空间”中。
In non-Internet Explorer browsers, I would recommend
getElementsByTagNameNS
to get the elements in a specific namespace.In Internet Explorer, you can use XPath instead.
jQuery also provides a way to use namespaces; it seems to be covered in "jQuery XML parsing with namespaces".
我相信以下功能应该有效 - 我在之前的项目中使用过它,并且我相信它在 Internet Explorer 6、7 和 8 中有效地工作。我没有在 Internet Explorer 9 中测试的好方法,但是我猜它应该可以正常工作,如 Internet Explorer 9 支持
getElementsByTagNameNS
。它非常简单,并且依赖于核心浏览器方法。这里唯一的痛点是需要定义从命名空间前缀到完整命名空间 URI 的映射。如果你想让它成为一个更可移植的函数,你可以将 nsMap 作为函数参数,而不是在函数体中定义的东西;或者您可以在全局上下文中引用名称空间映射对象。
这是一个完全模块化的版本,其中
getElementsByTagName
的版本稍微严格一些:I believe the following function should work - I've used it in a previous project, and I believe it works effectively in Internet Explorer 6, 7, and 8. I don't have a good way to test in Internet Explorer 9, but I'm guessing it should work properly, as Internet Explorer 9 supports
getElementsByTagNameNS
. It's pretty straightforward, and relies on core browser methods.The only pain point here is the requirement to define a mapping from the namespace prefix to the full namespace URI. If you want to make this a more portable function, you could have
nsMap
be a function argument, rather than something defined in the function body; or you could refer to a namespace map object in the global context.Here's a fully modularized version, with a slightly tighter version of
getElementsByTagName
:在 Internet Explorer 8 及更低版本中,元素的
canHaveChildren
属性为false
这意味着死胡同,浏览器根本不支持此标记具有子节点,同样的方式例如,
不能有子节点。不过,此问题已在 Internet Explorer 9 中得到修复。
In Internet Explorer 8 and below, the
canHaveChildren
property of the element isfalse
which means dead end, the browser simply doesn't support having child nodes for this tag, same way that<br />
can't have child nodes for example.This has been fixed in Internet Explorer 9 though.
我从来没有遇到过这个问题,所以这只是一个建议或提示。
我发现 MSIE 命名空间“限制”的“getElementsByTagName(W3C DOM 核心方法)”。
我终于在“XPath中找到了要点JavaScript,第 3 部分”将确认 XPath 的使用,这将是重点:
I never had this issue, so this is rather just a proposal or hint.
I found "getElementsByTagName (W3C DOM Core method)" for MSIE namespace "restriction".
I finally found the point in "XPath in JavaScript, Part 3" that would confirm XPath use would be the point:
这是一个 hack,但它可能足够强大以满足您的需要:
例如:
This is a hack, but it might be robust enough for what you need:
For example: