Gecko/Firefox 在 SVG 文档上缺少 document.getElementsByName
我有一个 SVG 文档,其中使用 JavaScript 来突出显示鼠标悬停时的元素。我的元素都有名称 - 有些元素具有相同的名称,因为尽管它们出现多次,但它们在逻辑上指的是同一事物。当鼠标悬停在复制元素的一个实例上时,我希望它们全部突出显示。
为了实现突出显示,我查找鼠标悬停的元素的名称。然后,我调用 document.getElementsByName()
来查找共享该名称的所有元素。使用返回的元素数组,我迭代地应用适当的样式来突出显示。
这在 WebKit 上运行良好,但在 Gecko 上失败 - 后者告诉我 getElementsByName
未定义。其中,查看 document
的函数表确实是这样:getElementsByClassName
、getElementsByTagName
、getElementsByTagNameNS
都在那里;缺少 getElementsByName
。
关于为什么 Gecko 忽略这个选择器有什么想法吗?谷歌在这件事上没有提供任何帮助(尽管我可能问了错误的问题)。
对于 Gecko 缺乏对此选择器的支持,有什么建议可以提供紧凑的解决方法吗?我非常希望不必劫持类属性或(更糟糕)为我的重复实例生成唯一的 id 来完成任务。
I have an SVG document in which I am using JavaScript to highlight elements on mouseover. My elements all have names - some elements have the same name because, although they appear multiple times, they logically refer to the same thing. When one instance of a replicated element is moused over, I want them all to highlight.
To effect the highlighting, I look up the name of the element being moused over. Then, I call document.getElementsByName()
to find all the elements sharing that name. With the returned array of elements, I iteratively apply the appropriate style to highlight.
That works great on WebKit and fails on Gecko - the latter informs me that getElementsByName
is undefined. Which, looking at the function table for document
is indeed the case: getElementsByClassName
, getElementsByTagName
, getElementsByTagNameNS
are all there; getElementsByName
is missing.
Any ideas as to why Gecko leaves this selector out? Google was unhelpful in this matter (though I may have asked the wrong questions).
Any suggestions for a compact workaround to Gecko's lack of support for this selector? I'd greatly prefer not having to hijack the class attribute or (worse) generate unique ids for my repeated instances to accomplish the task.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是:SVG 文档也是 (X)HTML 文档吗?
https://developer.mozilla.org/en/DOM/document.getElementsByName
自由贸易协定:
The question is: is an SVG document also a (X)HTML document?
https://developer.mozilla.org/en/DOM/document.getElementsByName
FTA:
@roatin-marth 让我在这里找到答案,但从未作为答案发布。我想我会在这里记录他的建议,以防对其他人有所帮助。
使用
querySelectorAll
提供了我一直在寻找的紧凑解决方法 - 它必须比getElementsByName
更强大(并且更灵活)。@roatin-marth put me on the path to an answer, here, but never posted as an answer. I thought I would capture his suggestion here in case it helps anyone else.
Using
querySelectorAll
provides the compact workaround I was seeking — it is must more robust (and more flexible) thangetElementsByName
.