为了账号安全,请及时绑定邮箱和手机立即绑定

var children = root.getElementsByName('div'); 为什么有问题 ?

var children = root.getElementsByName('div'); 为什么有问题 ?

19990000 2017-09-23 22:36:17
<!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <title>Title</title>     <style>         div{             width:50px;             height:50px;             border:1px solid #000000;         }     </style> </head> <body>     <div class="one">         1         <div class="two" style="margin-left: 50px;">             2             <div class="three"  style="margin-left: 50px;">                 3                 <div class="four"  style="margin-left: 50px;">4</div>             </div>         </div>         <div class="one">1</div>     </div> </body> <script>     var one=document.getElementsByClassName("one")[0];     var i;     function visit(node){         i +=400;         node.style.backgroundColor="#fff";         setTimeout(function(){             node.style.backgroundColor="#0f0";         },i)     }     function qx(root){         if (root) {  //如果根元素村子  visit(root);  //执行动画  var children = root.getElementsByName('div');   for (var i = 0; i < children.length; i++) {  //循环                 //只选择子一层div  if(children[i].parentNode == root) {   //将子元素等于根元素  qx(children[i]);  //那么就进行添加  }             }         }     }     qx(one) </script> </html>
查看完整描述

1 回答

?
fdsgasgg

TA贡献19条经验 获得超12个赞

getElementsByName是找name属性的


你要用的可能是getElementsByTagName

查看完整回答
1 反对 回复 2017-09-24
  • Willsf
    Willsf
    根本原因是root的原型对象里面没有getElementsByName属性,只有getElementsByTagName属性,楼主可以打印 console.dir(root.__proto__.__proto__.__proto__)展开里面只有getElementsByTagName属性,所以报错未定义
  • 1 回答
  • 0 关注
  • 1058 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信