经典面试题:求一个网页中出现次数最多的三种标签

 1        const html=document.querySelector('html')
 2        const htmlChild=html.children;
 3        let obj={};
 4        function fn(children){
 5         for(let i of children){
 6             if(obj.hasOwnProperty(i.tagName)){
 7                 obj[i.tagName]=obj[i.tagName]+1
 8             }else{
 9                 obj[i.tagName]=1;
10             }
11             const child=i.children;
12             if(child.length!==0){
13                 fn(child)
14             }
15         }
16        }
17        fn(htmlChild)
18        let tag=Object.entries(obj).sort((a,b)=>{
19            return b[1]-a[1]
20        })
21        console.log(tag)

猜你喜欢

转载自www.cnblogs.com/ayujun/p/12189720.html