Analysis of several methods of obtaining HTML elements in Web pages by JS

Source: Internet
Author: User
Tags tag name

getElementById getelementsbyname getelementsbytagname approximate introduction

getElementById, getelementsbyname, getelementsbytagname

# # #adv # #

The latter two are to get the set, Byid just get a single object

The use of getElementById

As an example:

<a id= "Link1" name= "link1" href=http://homepage.yesky.com> page Pottery bar </a>

Reference methods within the same page:

  1. Use ID:

Link1.href, the return value is http://homepage.yesky.com

  2. Use name:

Document.all.link1.href, the return value is http://homepage.yesky.com

  3. Using Sourseindex:

document.all (4). href//Note that there is also HTML, HEAD, title, and body in front, so it is 4

  4. Use the link collection:

Document.anchors (0). href

All the collections are all, anchors, applets, areas, attributes, Behaviorurns, bookmarks, boundelements, cells, childNodes, children, Controlrange, elements, embeds, filters, forms, frames, images, imports, links, mimetypes, options, plugins, rows, rules, Scripts, stylesheets, tbodies, Textrectangle, please refer to the MSDN introduction.

In fact, Method 3 and Method 4 are the same set of use, just one is all, can include all the markup of the page, and anchors only include links.

  5, getElementById:

document.getElementById ("Link1"). href

  6, Getelementsbyname:

Document.getelementsbyname ("Link1") [0].href//This is also a collection, which is a collection of all tags with name equal to the parameters of the method

  7, getElementsByTagName:

document.getElementsByTagName ("a") [0].href//This is also a collection, which is a collection of all markup names that are equal to the parameters of the method

  8. Tags collection:

Document.all.tags ("A") [0].href

As with Method 7, you get a collection by tag name

Besides:

Event.screlement can obtain a reference to the marker of the trigger time;

Document.elementfrompoint (x, y) can obtain a reference to the elements at x and y coordinates;

Document.body.componentFromPoint (Event.clientx,event.clienty) can get a reference to the element where the mouse is located;

It can also be referenced through the parent-child node and sibling node relationships of the elements, such as nextsibling (the next node of the current node), previoussibling (the previous node of the current node), childNodes, children, FirstChild, LastChild, parentelement, and so on are some references to parent-child nodes and sibling nodes, and not limited to this.

The above is a common reference method within the same page, and also involves different pages of the

Getelementsbyname returns a collection of all the elements that have a specified value for name

"Gets a collection of objects based on the value of the NAME tag property. ”

The collection is much looser than the array, and the type of each subkey in the collection can be different, and the collection simply puts some elements together as a class to use, in contrast to the array is more strict, each child is a unified type. Document.getelementsbyname, document.getElementsByTagName, document.formName.elements the results of such methods are set.

getElementById getelementsbyname getelementsbytagname approximate introduction

getElementById, getelementsbyname, getelementsbytagname

# # #adv # #

The latter two are to get the set, Byid just get a single object

The use of getElementById

As an example:

<a id= "Link1" name= "link1" href=http://homepage.yesky.com> page Pottery bar </a>

Reference methods within the same page:

  1. Use ID:

Link1.href, the return value is http://homepage.yesky.com

  2. Use name:

Document.all.link1.href, the return value is http://homepage.yesky.com

  3. Using Sourseindex:

document.all (4). href//Note that there is also HTML, HEAD, title, and body in front, so it is 4

  4. Use the link collection:

Document.anchors (0). href

All the collections are all, anchors, applets, areas, attributes, Behaviorurns, bookmarks, boundelements, cells, childNodes, children, Controlrange, elements, embeds, filters, forms, frames, images, imports, links, mimetypes, options, plugins, rows, rules, Scripts, stylesheets, tbodies, Textrectangle, please refer to the MSDN introduction.

In fact, Method 3 and Method 4 are the same set of use, just one is all, can include all the markup of the page, and anchors only include links.

  5, getElementById:

document.getElementById ("Link1"). href

  6, Getelementsbyname:

Document.getelementsbyname ("Link1") [0].href//This is also a collection, which is a collection of all tags with name equal to the parameters of the method

  7, getElementsByTagName:

document.getElementsByTagName ("a") [0].href//This is also a collection, which is a collection of all markup names that are equal to the parameters of the method

  8. Tags collection:

Document.all.tags ("A") [0].href

As with Method 7, you get a collection by tag name

Besides:

Event.screlement can obtain a reference to the marker of the trigger time;

Document.elementfrompoint (x, y) can obtain a reference to the elements at x and y coordinates;

Document.body.componentFromPoint (Event.clientx,event.clienty) can get a reference to the element where the mouse is located;

It can also be referenced through the parent-child node and sibling node relationships of the elements, such as nextsibling (the next node of the current node), previoussibling (the previous node of the current node), childNodes, children, FirstChild, LastChild, parentelement, and so on are some references to parent-child nodes and sibling nodes, and not limited to this.

The above is a common reference method within the same page, and also involves different pages of the

Getelementsbyname returns a collection of all the elements that have a specified value for name

"Gets a collection of objects based on the value of the NAME tag property. ”

The

Collection is much looser than the array, and the type of each subkey in the collection can be different, and the collection simply puts some elements together as a class to use, in contrast to the array is more strict, each child is a unified type. Document.getelementsbyname, document.getElementsByTagName, document.formName.elements The result of such a method is a collection.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.