document.getElementsByName missing from Gecko/Firefox on SVG documents
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 effe开发者_高级运维ct 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.
The question is: is an SVG document also a (X)HTML document? https://developer.mozilla.org/en/DOM/document.getElementsByName
FTA:
The name attribute is only applicable to (X)HTML documents. The method returns all elements with a name attribute, such as or or even if name is placed on elements which do not support a name attribute at all.
@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) than getElementsByName
.
精彩评论