js如何获取只class为a的元素,不包括.a.b的元素

js如何获取只class为a的元素,不包括.a.b的元素?
例如:

<div class="a b"></div><div class="a"></div>


蓝山帝景
浏览 1194回答 1
1回答

Qyouu

&nbsp; &nbsp; &nbsp; &nbsp; var elems = document.getElementsByTagName("div");&nbsp; &nbsp; &nbsp; &nbsp; var arr = [];&nbsp; &nbsp; &nbsp; &nbsp; for(var i = 0,len = elems.length;i<len;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(elems[i].className.indexOf("a")>=0&&elems[i].className.indexOf("b")<0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr.push(elems[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; alert(arr.length);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript