JS to obtain HTML objects in several ways

  • 2020-03-30 00:43:46
  • OfStack

Document. The getElementById (" zx ");

Get the HTML element object by ID, which should be unique in the HTML document. Returns a unique element object. And all browsers are compatible.

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

HTML objects are found by tags, and because HTML tags can be repeated many times in a page, the current page returns an array. You can locate the object of an element based on where the label appears. All browsers are compatible.


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

The name attribute is used to locate the HTML object, but not all tags have the name attribute, but we can artificially add the name attribute, so we can also locate, because the name attribute may be multiple, not unique. So this method also returns an array, and we can also position the name based on the position of the name in the HTML. The IE series is not compatible and is not recommended.


InnerHTML: this method gets the HTML code for the HTML element

Document. GetElementById (" zx "). The innerHTML;

You can also update the HTML code for the element:

Document. The getElementById (" zx "). The innerHTML = "< B> Let's go on a date. / b>" ;


Related articles: