Browser supports querySelectorAll
document.querySelectorAll("h1, h2, h3, h4")
Copy the code
If it’s a jQuery
$("h1, h2, h3, h4, h5, h6")
Copy the code
2, JQuery
var tags = [ "h1","h2","h3","h4" ];
var h_all = [];
for(var i = 0; i < tags.length; i++){
h_all = h_all.concat(document.getElementsByTagName(tags[i]));
}
Copy the code
3. Pure JS solutions
for (i=1; i<=4; i++) { var headers = document.getElementsByTagName('h'+i); for (j=0; j<headers.length; J ++) {headers[j]. ClassName = 'h '; }} var hClass = document. GetElementsByClassName (' h - class '); for (i=0; i<hClass.length; i++) { hClass[i].innerHTML += ' '+i; }Copy the code
There are no JQuery and querySelectorAll functions
function getAllH(document) { var arr = []; if (! document) return arr; var elementAll = document.getElementsByTagName('*'); for (var i = 0, n = elementAll.length; i < n; i++) { if (/^h\d{1}$/gi.test(elementAll[i].nodeName)) { arr.push(elementAll[i]); } } return arr; }Copy the code