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 ~";