简体   繁体   English

使用数组之外的childNodes初始化变量

[英]Initializing a variable with childNodes out of an array

Can someone help explain what the following JavaScript syntax was meant to do: 有人可以帮忙解释以下JavaScript语法的含义吗?

  var tagLink_ar = document.getElementsByTagName('td');  

  **var x = tagLink_ar.childNodes, i=0, j;**                 // Not sure ???

This is from an older javascript function that no longer works in IE>9 or FF. 这来自较旧的javascript函数,该函数在IE> 9或FF中不再起作用。

tagLink_ar of course is the array of TD tags, yet I have not previously seen a variable with an index element i and j, and can't find a similar descriptor in my research online. tagLink_ar当然是TD标签的数组,但是我以前从未见过带有索引元素i和j的变量,并且在我的在线研究中找不到相似的描述符。 Does i just reference the starting element and j the length? 我是否仅引用起始元素,并且j引用长度?

Thoughts on how to load x in a similar manner? 关于如何以类似方式加载x的想法?


Makes sense it is a simple declaration for x, i and j. 这是对x,i和j的简单声明,这很有意义。 Yet getting a "TypeError: x is undefined". 却收到“ TypeError:x未定义”。

Below is the function as a reference: 下面是该函数的参考:

var tagLink_ar = document.getElementsByTagName('td');  
**var x = tagLink_ar.childNodes, i=0, j;**                 // Not sure ???

while(j == x[i++]){                                             
  if(j.nodeType == 1 && nodeName == 'div'){                     
     var viewDiv = getStyle(divElement, 'display');
     if (viewDiv == 'block'){                                   
       x[j].style.borderBottom = "1px solid #000000";
     }
   }
}

Noted the syntax error on the while, should have been j=x[i++], and j.nodeName 注意while上的语法错误,应该是j = x [i ++]和j.nodeName

There is no reasonable circumstance where x could have a childNodes property. x可能具有childNodes属性是没有合理的情况的。 tagLink_ar is a NodeList , and each of the elements in that list has a childNodes property. tagLink_ar是一个NodeList该列表中的每个元素都有一个childNodes属性。

There are a few other reasons why that code would never work. 还有其他一些原因导致该代码无法正常工作。 Did you copy it correctly? 您复制正确吗?

To get the result the code was designed to have, you can do this (I'm including some sample HTML so that you can run it as a snippet): 要获得代码设计所要具有的结果,可以执行以下操作(我包含一些示例HTML,以便可以将其作为摘要运行):

 var tagLink_ar = document.getElementsByTagName('td'), children, i, j, node, viewDiv; for (i = 0; i < tagLink_ar.length; i += 1) { children = tagLink_ar[i].childNodes; for (j = 0; j < children.length; j += 1) { node = children[j]; if (node.nodeType === 1 && node.nodeName.toLowerCase() === 'div' && node.style.display === 'block') { node.style.borderBottom = "1px solid #000000"; } } } 
 <table> <tr> <td> <div>a</div> </td> <td> <div style="display: block;">b</div> </td> </tr> <tr> <td> <div style="display: block;">c</div> </td> <td> <div style="display: block;">b</div> </td> </tr> <tr> <td> <div>e</div> </td> <td> <div>f</div> </td> </tr> </table> 

The syntax in question just initializes variables, albeit very confusingly. 有问题的语法只是初始化变量,尽管非常令人困惑。

var x = tagLink_ar.childNodes, i=0, j;

Is equivalent to 相当于

var x = tagLink_ar.childNodes;
var i = 0;
var j = undefined;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM