Get elements by tag name--
getElementsByTagName
Standard
- DOM 1 is defined in and two interface, and the prototypes indicate the order in which they are
Element
Document
encountered in NodeList getElementsByTagName(in DOMString tagname)
the first order traversal, no exceptions are thrown, and parameters "*"
return all elements under the document or element. Notice here that the returned is a live only containing Element
NodeList
.
- The DOM 2 definition is still in
Element
and Document
, added the concept of band namespace ( NodeList getElementsByTagNameNS(in DOMString namespaceURI, in DOMString localName)
Element
, Document
), introduced localName
(only ELEMENT_NODE
and ATTRIBUTE_NODE
talented).
- DOM 3 (
Document
, Element
) specifically declares that XML should be sensitive to the case of the label name, while non-XML determines whether it is sensitive according to the style of the document type's own case. In fact, the browser to HTML will first convert the label into lowercase and then go to match, so only the actual tag is named lowercase elements.
- WHATWG (
Document
, Element
) will return the type modification in order to HTMLCollection
, and explain the localName
algorithm produced by HTMLCollection
the. Note that the second step of the algorithm actually specifies that the label name is sensitive in non-HTML documents, whereas in an HTML document, any case-size label will be converted to lowercase and then matched, so it can only match to an element with the actual label named lowercase.
- DOM 4 (document,element) is now basically consistent with WHATWG
DOM Tree Accessors
Dom 1 and Dom 2 HTMLDocument
interface define a series of "Dom Tree accessors"
readonly attribute HTMLCollection images
readonly attribute HTMLCollection applets
readonly attribute HTMLCollection links
readonly attribute HTMLCollection forms
readonly attribute HTMLCollection anchors
attribute HTMLElement body
This means that in an HTML document, you can get all the elements, get all the elements with attributes, get all document.images
document.links
href
<a>
document.links
name
The elements with attributes <a>
, and document.forms
get all <form>
the Elements. Additionally the get <body>
can be used document.body
.
Since HTML5 (W3C,WHATWG) has also been defined document.head
, deleted, document.anchors
increased acquired <embed>
document.embeds
and present with the same document.plugins
, as well as obtained <script>
document.scripts
.
Since DOM 1 is Document
defined document.documentElement
to get the root element and remains so far in the HTML document, the element.
Compatibility
- IE 5.5 does not support
*
getting all elements as parameters. More than IE6 IE and the major browsers are implemented according to the standard getElementsByTagName
.
- Although
document.scripts
, document.embeds
and document.plugins
until HTML5 is standardized, the current versions of each version of IE and other browsers support
document.head
Need ie9+ to support. Other Dom Tree accessor are basically supported in all versions of IE and in the current browser.
Webkit Code AnalysisSimilar getElementsByName
, getElementsByTagName
ContainerNode
implemented in. Because of the special provisions of XML in the standard, this will be based on the document type, swap TagNodeList
or HTMLTagNodeList
as NodeListsNodeData::addCacheWithAtomicName<>
the template specialization (see WEBCORE/DOM/CONTAINERNODE.CPP).
TagNodeList
elementMatches
This is achieved by:
if (M_localname! = Staratom && m_localname! = Element.localname ( )) return false ; return M_namespaceuri = = Staratom | | M_namespaceuri = = Element.namespaceuri ();
This starAtom
is what the standard says *
. Whether or not the LocalName matches or is *
, and then is better than the NamespaceURI *
. Because there is no case conversion step, following the standard is case-sensitive. Note that this is more than the NamespaceURI of the steps relative to getElementsByTagName
is redundant, the reason is added because it is getElementsByTagNameNS
also used TagNodeList
, so you can be lazy to write a more than the NamespaceURI version. But the getElementsByTagNameNS
use of the fact is addCacheWithQualifiedName
not addCacheWithAtomicName
, in fact, addCacheWithQualifiedName
and addCacheWithAtomicName
the difference is that it takes TagNodeList
the direct advance of the template specification only (see webcore/dom/ NODERAREDATA.H)
HTMLTagNodeList
elementMatches
This is achieved by:
if (M_localname = = Staratom ) return true ; Const atomicstring& localname = Element.ishtmlelement ()? m_loweredlocalname:m_localname; return LocalName = = Element.localname ();
According to the standard, if the element being compared is in HTML namespace, it is converted to lowercase and then compared. Note There is no comparison to NamespaceURI, after all it is not getElementsByTagNameNS
used (the standard does not specify the getElementsByTagNameNS
need to convert the case, so use TagNodeList
that case sensitive filter enough).
Follow the standard and WebKit source to explore DOM--get the getElementsByTagName of the element