如果我试图找到绿色或红色等的数量。 我应该对此进行字符串解析吗:
document.getElementById("targetElement").getAttribute("material").color
输出类似于"rgb(0,0,0)"
,因此我可以使用正则表达式匹配来查找绿色。有没有更好的方法可以做到这一点?
THREE.Color
实用程序 https://threejs.org/docs/api/math/Color.html
THREE.Color('rgb(10, 20, 30)').r // 10
试试那个:
var greenElements = document.querySelectorAll("*").filter(function () {
return this.getAttribute("material") == 'rgb(0,255,0)'
});
console.log(greenElements.length);
如果您了解有关要搜索的元素的更多信息,则可以更改选择器。我使用了所有匹配的选择器,如果您有很多 html 要解析,可能会"慢"。