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