About getelementsbytagname ()

Source: Internet
Author: User
Document directory
  • Syntax
  • Description
  • Example 1
  • Example 2
  • Example 3
Definition and usage

The getelementsbytagname () method returns a set of objects with the specified tag name.

Syntax
document.getElementsByTagName(tagname)
Description

The getelementsbytagname () method returns the order of elements in the document.

If you pass the special string "*" to the getelementsbytagname () method, it returns a list of all elements in the document. The order of elements is the order in the document.

Tips and comments

Note: strings passed to the getelementsbytagname () method can be case-insensitive.

Example 1
document.getElementsByTagName("input");  alert(x.length);  }</script>Example 2

You can use the getelementsbytagname () method to obtain the list of HTML elements of any type. For example, the following code obtains all the tables in the document:

var tables = document.getElementsByTagName("table");alert ("This document contains " + tables.length + " tables");
Example 3

If you are familiar with the structure of the document, you can also use the getelementsbytagname () method to obtain a specific element in the document. For example, the following code obtains the fourth section of the document:

var myParagragh = document.getElementsByTagName("p")[3];

However, we still think that if you need to operate a specific element, the getelementbyid () method will be more effective.

Function queding () {var val = ""; var allclass =$ ('Type _ left '). getelementsbytagname ('img '); For (VAR I = 0; I <allclass. length; I ++) {If (allclass [I]. classname = "app-icon-seldw") {if (I <= 5) {Val + = "road construction -";} else if (I> = 14 & I <= 50) {Val + = "rural road-";} else if (I> = 53 & I <= 62) {Val + = "highway maintenance-";} else {Val + = "Other-";} Val + = allclass [I]. nextsibling. firstchild. nodevalue + ";";}}

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.