我可以使用getElementsByTagName选择多个标签吗? [英] Can I select multiple tags using getElementsByTagName?
问题描述
我正在使用JavaScript代码段,以便我的网站访问者使用以下javascript增加所有段落的字体大小:
function incrementFontSize(){
var段= document.getElementsByTagName('p'); $(b
$ b for(i = 0; parseInt函数(段落[I] .style.fontSize.replace( PX, ));
} else {
var s = 14;
}
if(s!= max){
s + = 1;
}
段落[i] .style.fontSize = s +px
}
}
如何在此代码中加入li,以便p和li是受影响的选定元素?
我也想避免在我的li或ul中添加一个类或一个id。有没有办法一次选择两个标签?
不,您不能选择多个标签的getElementsByTagName
。您可以使用 getElementsByTagName
或使用 querySelectorAll
来执行两个查询。
var elems = document.querySelectorAll('p,li')
I'm using a javascript snippet in order for visitors to my site to increase the font size on all paragraphs using the following javascript:
function increaseFontSize() {
var paragraphs = document.getElementsByTagName('p');
for(i=0;i<paragraphs.length;i++) {
if(paragraphs[i].style.fontSize) {
var s = parseInt(paragraphs[i].style.fontSize.replace("px",""));
} else {
var s = 14;
}
if(s != max) {
s += 1;
}
paragraphs[i].style.fontSize = s+"px"
}
}
How can I also include "li" to this code so that "p" and "li" are the selected elements that are affected?
I would also like to avoid adding a class or an id to my "li" or "ul". Is there a way to select two tags at once?
No, you can't select multiple tags with a single call to getElementsByTagName
. You can either do two queries using getElementsByTagName
or use querySelectorAll
.
var elems = document.querySelectorAll('p,li')
这篇关于我可以使用getElementsByTagName选择多个标签吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!