前端面试题:统计每一个标签出现的次数
<!DOCTYPE html> <html> <head> <title>题目:统计标签的个数</title> </head> <body> <div> <ul> <li><br></li> <li></li> </ul> <br> <section> <p> <span></span> <b></b> <article></article> </p> </section> </div> <script> 'use strict'; function foo(){ var target, o, i = 0, clone; if(typeof arguments[0] == 'boolean'){ o = arguments[1]; target = arguments[2] }else{ o = {}; target = arguments[0]; } var children = target.children; var len = children.length; o[target.nodeName] = 1; for(;i<len;i++){ var child = children[i]; if(child.children.length){ foo(true, o, child); }else{ var nodename = child.nodeName; o[nodename] = o[nodename] ? o[nodename] + 1 : 1; } } return o; } console.log(foo(document.querySelector('div'))) </script> </body> </html>