Home  >  Article  >  Web Front-end  >  Introduction to several ways to obtain html objects in JS_javascript skills

Introduction to several ways to obtain html objects in JS_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:10:38971browse

document.getElementById("zx");

Get the html element object by ID. The ID number should be unique in the html document. What is returned is the only element object. And all browsers are compatible.

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

Find html objects through tags. Since html tags may be repeated many times in a page, the current page returns an array. An object that can position elements based on where the label appears. All browsers are compatible.


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

Locate the html object through the name attribute, but not all tags have the name attribute, but we can artificially add the name attribute, so that it can also be located. Since there may be multiple name attributes, they are not unique. So this method also returns an array. Similarly, we can also position the name according to the position of the name in the html. The IE series is not compatible and is not recommended.


innerHTML: This method is to get 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 go on a date~";

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn