如何使用javascript来计算DIV标记的数量?唯一的属性将永远是style="font-family:Courier New;"
<div style="font-family:Courier New;">Some text</div>
<div style="font-family:Courier New;">Some text</div>
<div style="font-family:Courier New;">Some text</div>
这一页上还有其他的设计信息。我只想把目标对准那些有style="font-family:Courier New;"
帮助的人?
发布于 2016-02-01 14:40:43
你在找querySelectorAll()
var courier = document.querySelectorAll('div[style="font-family:Courier New;"]');
for (var i = 0, len = courier.length; i < len; i++) {
courier[i].style.color = 'red';
}
<div style="font-family:Courier New;">Some text</div>
<div style="font-family:Courier New;">Some text</div>
<div style="font-family:Arial;">Some text</div>
<div style="font-family:Courier New;">Some text</div>
现在,您可以像在CSS中那样编写一个查询。
适用于所有主要浏览器:http://caniuse.com/#feat=queryselector
发布于 2016-02-01 14:37:38
您可以使用jQuery来完成这个任务:
console.log($("[style='font-family:Courier New;']").length);
https://stackoverflow.com/questions/35133113
复制相似问题