此 Javascript 代码段的 W3C 验证错误

W3C Validation Errors with this Javascript snippet

本文关键字:W3C 验证 错误 Javascript 代码      更新时间:2023-09-26

我在这个Javascript代码中遇到了w3c验证错误,想知道一个善良的绅士/女士是否会给我一点时间。

// hide all element nodes within some parent element
function hideAll(parent) {
    var children = parent.childNodes, child;
    // loop all the parent's children
    for (var idx=0, len = children.length; idx<len; ++idx) { /* ERROR HERE */
        child = children.item(idx);
        // if element node (not comment- or textnode)
        if (child.nodeType===1) {
            // hide it
            child.style.display = 'none';
        }
    }
}

错误是:

  • 元素 "len" 未定义
  • 字符 ";" 属性规范列表中不允许

idx<len;处的分号是出错的地方。

有人可以解释一下上面的代码片段哪里出了问题吗?

非常感谢。

将其更改为:

// hide all element nodes within some parent element
function hideAll(parent) {
    var children = parent.childNodes, child;
    // loop all the parent's children
    var len = children.length;
    for (var idx=0; idx<len; ++idx) { /* ERROR HERE */
        child = children.item(idx);
        // if element node (not comment- or textnode)
        if (child.nodeType===1) {
            // hide it
            child.style.display = 'none';
        }
    }
}
          **// hide all element nodes within some parent element

             function hideAll(parent) 
             {
                var children = parent.childNodes, child;
                // loop all the parent's children
                var len=children.length;
                 for (var idx=0; idx<len; ++idx) 
                 { /* ERROR HERE */
                   child = children.item(idx);
                    // if element node (not comment- or textnode)
                     if (child.nodeType===1) 
                     {
                         // hide it
                         child.style.display = 'none';
                     }
                } 
         }**