首页 > 试题广场 >

写一个traverse函数,输出所有页面宽度和高度大于50像

[问答题]
写一个traverse函数,输出所有页面宽度和高度大于50像素的节点。
function traverse(){
var arr =[];
var res =[];
arr = document.getElementsByTagName("*");
console.log(arr);
for(var i =0;i<arr.length;i++){
if(arr[i].offsetHeight>50 &&arr[i].offsetWidth >50){
res.push(arr[i]);
}
}
return res;
}
发表于 2015-10-28 19:40:07 回复(0)
function getCurrentStyle(node,attr){
    if(node.currentStyle){
        return node.currentStyle.attr;
    }else{
        return getComputedStyle(node,nill)["attr"];
    }
}
function traverse(nodes){
    var arr=[];
    for(var i=0,len=nodes.length;i<len;i++){
        var node_width=paseInt(getCurrentStyle(nodes[i],width));
        var node_height=paseInt(getCurrentStyle(nodes[i],height));
        if(node_width<=50&&node_height<=50){
            arr.push(nodes[i]);
        }
        if(nodes[i].hasChildNodes()){
            arr=arr.concat(traverse(nodes[i].children));
        }
    }
    return arr;
}
traverse(document.body.children);

发表于 2015-09-14 11:31:28 回复(0)
function traverse(oNode) {
    var aResult = [];
    oNode = oNode || document.body;
    if (oNode.style) {
        var nWidth = window.parseInt(oNode.style.width, 10) || 0;
        var nHeight = window.parseInt(oNode.style.height, 10) || 0;
        if (nWidth > 50 && nHeight > 50) {
            aResult.push(oNode);
        }
    }
    var aChildNodes = oNode.childNodes;
    if (aChildNodes.length > 0) {
        for (var i = 0, l = aChildNodes.length; i < l; i++) {
            var oTmp = aChildNodes[i];
            aResult = aResult.concat(traverse(oTmp));
        }
    }
    return aResult;
}
发表于 2015-08-20 10:21:31 回复(4)
function tranverse() {
	let arr = []
    function tranverseRecur(nodes) {
        Array.from(nodes).forEach(node=> {
            if(node.offsetWidth > 50 && node.offsetHeight > 50){
                arr.push(node)
            }
            if(node.childNodes && node.childNodes.length > 0) {
                tranverseRecur(node.childNodes);
            }
        })
    }
    let nodes = (document.body.childNodes);
    tranverseRecur(nodes);
	return arr;
}

发表于 2019-11-10 22:05:48 回复(0)
function traverse(){
    return Array.prototype.filter.call(document.querySelectorAll('body *'), function(node){
        return node.offsetWidth > 50 && node.offsetHeight > 50;
    });
}

编辑于 2015-10-27 16:33:33 回复(8)
function traverse(){
    var eleArr = document.querySelector("body").children;
    var arr = [];
    for(var i = 0;i < eleArr.length;i++){
        if(eleArr[i].offsetWidth > 50 && eleArr[i].offsetHeight > 50){
            arr.push(eleArr[i])
        }
    }
    return arr;
}
发表于 2019-02-25 22:30:21 回复(0)
function traverse(){
return Array.from(document.getElementsByTagName("*")).filter(a=>a.offsetWidth>50&&a.offsetHeight>50)
}

编辑于 2018-12-10 14:41:58 回复(0)
function traverse(el) {
if (!el) {
el = document.getElementsByTagName('body')[0]
}
var style = window.getComputedStyle(el)
if (style.width.replace('px', '') > 50 && style.height.replace('px', '') > 50) {
console.log(el)
}
if (el.children) {
var index = -1
while (++index < el.children.length) {
traverse(el.children[index])
}
}
}

发表于 2018-06-22 14:44:31 回复(0)
function travaerse() {
    var body = document.body;
    var nodes = [];
    var filter = {
        acceptNode(node) {
            let style = document.defaultView.getComputedStyle(node, null);
            if (Number.parseFloat(style.width) > 50 && Number.parseFloat(style.height) > 50) {
                return NodeFilter.FILTER_ACCEPT;
            } else {
                return NodeFilter.FILTER_SKIP;
            }
        }
    }
    var iterator = document.createNodeIterator(body, NodeFilter.SHOW_ELEMENT, filter, false);
    var node = iterator.nextNode();
    while (node !== null) {
        nodes.push(node);
    }
    return nodes;
}


编辑于 2017-08-21 21:02:45 回复(0)
function traverse() { var tags = document.getElementsByTagName('*'); var tagLen = tags.length; for (var i = 0; i < tagLen; i++) { var curr = tags[i]; var width = curr.clientWidth,
                height = curr.clientHeight; if (width > 50 && height > 50) { console.log(curr);
        }
    }
}

发表于 2017-03-20 16:23:34 回复(0)
function traverse() {
var tag = document.getElementsByTagName("*");
for(var i=0; i< tag.length; i++) {
var width = parseInt( tag[i].style.width);
var height = parseInt(tag[i].style.height);
if(width > 50 && height >50 )
{console.log(tag[i]);}
}
}


发表于 2017-02-15 20:40:30 回复(0)
function traverse(){
    var eles = document.getElementsByTagName('*'),
          style,
          width,
          height,
          res = [];
    
    Array.prototype.forEach.call(eles, function(value){
        style = window.getComputedStyle(value);
        width = parseInt(style.width);
        height = parseInt(style.height);

        width > 50 && height > 50 && res.push(value);
    });

    return res;
}
发表于 2017-02-12 17:12:53 回复(0)
docu
发表于 2017-02-11 00:44:30 回复(0)
function traverse() {
  var ele = document.getElementsByTagName('*');
  return Array.prototype.filter.call(ele,function(item) {
    return item.offsetWidth > 50 && item.offsetHeight > 50;
  })
}

发表于 2016-10-08 22:18:48 回复(0)
function traverse() {
    var arr = [];
    var elements = [];
    if (document.all) {
        elements = document.all;
    } else {
        elements = document.getElementsByTagName("*");
    }
    for (var i = 0; i < elements.length; i++) {
        var ele = elements[i];
        var width = parseFloat(ele.style.width) || ele.offsetWidth;
        console.log(width);
        var height = parseFloat(ele.style.height) || ele.offsetHeight;
        console.log(height);
        if (width > 50 && height > 50) {
            arr.push(elements[i]);
        }
    }
    return arr;
}
发表于 2016-09-06 18:52:01 回复(0)

concat() 方法用于连接两个或多个数组。

该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本。

发表于 2016-08-25 15:48:11 回复(0)
function traverse(){
    var arr[];
    function found(node){
        if(node.children.length!=0){
              for(var i=0;i<node.children.length;i++){
                  var child=node.children[i];
                  if(child.clientWidth>50&&child.clientHeight>50){
                      arr.push(child);
                  }
                 found(child) 
            }
        }
     }
    found(document)
    console.log(arr)
}

发表于 2016-08-02 14:33:06 回复(0)
!DOCTYPE html>
<html>
 
<head>
    <meta charset="utf-8">
    <title>统计Element节点</title>
    <script language="javascript">
        function traverse() {
            var arr = [];
            var elements = [];
            if (document.all) {
                elements = document.all;
            } else {
                elements = document.getElementsByTagName("*");
            }
            for (var i = 0; i < elements.length; i++) {
                var ele = elements[i];
                var width = parseFloat(ele.style.width) || ele.offsetWidth;
                var height = parseFloat(ele.style.height) || ele.offsetHeight;
                if (width > 50 && height > 50) {
                    arr.push(elements[i]);
                }
            }
            return arr;
        }
        onload = function() {
            console.log(traverse());
        }
    </script>
    <style type="text/css">
        #demo {
    border: #000 2px solid;
    background-color: #fff;
    width: 100px;
    height: 100px;
}
    </style>
</head>
 
<body>
    <h1>测试</h1>
    <table width="100" height="100" border="2" cellpadding="0" cellspacing="0">
        <tr>
            <td>
                <form name="form1" action="" method="post">
                    <input type="text" name="ipput1" value="测试">
                    <br />
                    <input type="password" name="password" value="">
                </form>
            </td>
        </tr>
    </table>
    <div id="demo"></div>
</body>
 
</html>
发表于 2016-04-16 19:50:35 回复(0)
function traverse (){
    function walk(element){
    
    if(element&&element.nextElementSibling){
        walk(element.nextElementSibling);
    }

    if(element&& element.firstElementChild){
        walk(element.firstElementChild );
    }

        if(element.offsetHeight>50){
        element.style.border="1px red"
        console.log(element);
    }
}
      
    walk(document.body);
    
}
编辑于 2016-03-17 01:53:33 回复(0)
function traverse(elem){
	var nodeArr = []
	recursive(elem)
	function recursive(poNode){
		var chileElems = poNode.children,
			len =chileElems.length,
			i = 0;
			h = poNode.offsetHeight,
			w = poNode.offsetWidth;
		if( h > 50 && w > 50 ){
			nodeArr.push(poNode)
		}
		if( len != 0 ){
			for( ; i < len; i++ ){
				recursive(chileElems[i])
			}
		}
	}
		return nodeArr;
}
console.log(traverse(document.body));

发表于 2015-10-25 12:56:30 回复(0)