HTML5 new javascript Selector

Source: Internet
Author: User

New Selector
Document. querySelector ("selector ");
Selector: returns the first matched element based on the CSS selector. If no matching element exists, null is returned;
Supported: Chrome 4.0 +, FireFox 3.5 +, Safari 3.2 +, Opera 10.1 +, IE 8 +

Document. querySelectorAll ("selector ");
Selector: returns an array of all matched elements based on the CSS selector. If no matching element exists, an empty array is returned;
Supported: Chrome 4.0 +, FireFox 3.5 +, Safari 3.2 +, Opera 10.1 +, IE 8 +

Document. getElementsByClassName ("selector ");
Selector: returns an array of all matched elements based on the class selector. If no array is matched, an empty array is returned;
Supported: Chrome 4.0 +, FireFox 3.0 +, Safari 3.2 +, Opera 10.1 +, IE 8 +

The example is as follows:
The HTML structure is as follows:

1 <div class = "content"> 2 <ul> 3 <li> instance </li> 4 5 <li class = "exp"> instance </li> 6 7 <li class = "exp"> instance </li> 8 9 <li class = "exp"> instance </li> 10 11 <li> instance </li> 12 13 </ul> 14 15 </div>

 

1. To obtain the first li element, we only need:

Document. querySelector (". content ul li ");

2. To obtain all the li elements, we only need:

Document. querySelectorAll (". content ul li ");

3. If you want to obtain all the li elements whose class is w3c, you only need:

Document. getElementsByClassName ("w3c ");

New Selector
Document. querySelector ("selector ");
Selector: returns the first matched element based on the CSS selector. If no matching element exists, null is returned;
Supported: Chrome 4.0 +, FireFox 3.5 +, Safari 3.2 +, Opera 10.1 +, IE 8 +

Document. querySelectorAll ("selector ");
Selector: returns an array of all matched elements based on the CSS selector. If no matching element exists, an empty array is returned;
Supported: Chrome 4.0 +, FireFox 3.5 +, Safari 3.2 +, Opera 10.1 +, IE 8 +

Document. getElementsByClassName ("selector ");
Selector: returns an array of all matched elements based on the class selector. If no array is matched, an empty array is returned;
Supported: Chrome 4.0 +, FireFox 3.0 +, Safari 3.2 +, Opera 10.1 +, IE 8 +

The example is as follows:
The HTML structure is as follows:

1 <div class = "content"> 2 <ul> 3 <li> instance </li> 4 5 <li class = "exp"> instance </li> 6 7 <li class = "exp"> instance </li> 8 9 <li class = "exp"> instance </li> 10 11 <li> instance </li> 12 13 </ul> 14 15 </div>

 

1. To obtain the first li element, we only need:

Document. querySelector (". content ul li ");

2. To obtain all the li elements, we only need:

Document. querySelectorAll (". content ul li ");

3. If you want to obtain all the li elements whose class is w3c, you only need:

Document. getElementsByClassName ("w3c ");

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.