使用此Javascript代码段的W3C验证错误

时间:2012-04-26 10:03:08

标签: javascript validation

我正在使用这个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”undefined
  • 字符“;”不允许在属性规范列表中

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

有人可以通过上面的代码片段解释我的错误吗?

非常感谢。

2 个答案:

答案 0 :(得分:1)

将其更改为:

// 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';
        }
    }
}

答案 1 :(得分:1)

          **// 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';
                     }
                } 
         }**