Home > Web Front-end > JS Tutorial > 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
Release: 2016-05-16 17:10:38
Original
1044 people have browsed it

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

Related labels:
js
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template