How to Get html objects using JS _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces several methods for JS to obtain html objects. For more information, see. Document. getElementById ("zx ");

The ID is used to obtain the html Element Object. the ID number must be unique in the html document. The returned object is a unique element. And all browsers are compatible.

Document. getElementsByTagName ("span") [0];

Search for html objects using tags. Because html tags may be repeated many times on a page, the current page returns an array. You can locate the object of an element based on the position where the tag appears. All browsers are compatible.


Document. getElementsByName ("hh") [0];

You can use the name attribute to locate html objects. However, not all tags have the name attribute. However, you can manually add the name attribute, because there may be multiple name attributes, they are not unique. Therefore, this method also returns an array. We can also locate the name based on the location of the name in the html. The IE series is not compatible and is not recommended.


InnerHTML: This method is used to obtain the specific html code of the html element.

Document. getElementById ("zx"). innerHTML;

You can also update the html code of the element:

Document. getElementById ("zx"). innerHTML ="Let's make a appointment ~";

Related Article

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.