Firefox 또는 JavaScript, Dom을 계산하십시오
-
19-09-2019 - |
문제
나는 이것이 간단하다고 확신하지만 어떻게 해야할지 모르겠다. HTML 페이지에서 DOM 요소의 양을 어떻게 계산합니까? 나는 사용자 스크립트 나 북마크에서 이것을하고 싶었지만 어떻게 시작 해야할지 전혀 모른다!
해결책
이것을 사용하십시오 Element
노드 :
document.getElementsByTagName("*").length
모든 노드의 경우 확장 할 수 있습니다 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;
};
그런 다음 전화하는 것만으로도됩니다 document.countChildNodes
.
다른 팁
// 동일한 방법을 사용하여 각 태그의 수를 얻을 수 있습니다.
function tagcensus(pa){
pa= pa || document;
var O= {},
A= [], tag, D= pa.getElementsByTagName('*');
D= A.slice.apply(D, [0, D.length]);
while(D.length){
tag= D.shift().tagName.toLowerCase();
if(!O[tag]) O[tag]= 0;
O[tag]+= 1;
}
for(var p in O){
A[A.length]= p+': '+O[p];
}
A.sort(function(a, b){
a= a.split(':')[1]*1;
b= b.split(':')[1]*1;
return b-a;
});
return A.join(', ');
}
알림 (tagcensus ())
JavaScript에서는 할 수 있습니다
document.getElementsByTagName("*").length
jQuery에서 당신은 할 수 있습니다
jQuery('*').length
제휴하지 않습니다 StackOverflow