23 javascript firefox dom bookmarklet userscripts
我确信这很简单,但我不知道该怎么做.我如何计算HTML页面中DOM元素的数量?我想在用户脚本或书签中这样做,但我不知道如何开始!
Gum*_*mbo 60
用于Element节点:
document.getElementsByTagName("*").length
Run Code Online (Sandbox Code Playgroud)
对于任何节点,您可以Node像这样扩展:
Node.prototype.countChildNodes = function() {
return this.hasChildNodes()
? Array.prototype.slice.apply(this.childNodes).map(function(el) {
return 1 + el.countChildNodes();
}).reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
})
: 0;
};
Run Code Online (Sandbox Code Playgroud)
然后你需要做的就是打电话document.countChildNodes.