2015-10-26 102 views
0

有人可以告诉我下面的代码有什么问题吗?它不会运行。 startNode.classList的返回值是什么?element.classList的输出是什么?

var startNode = document.body 
if (startNode.classList.contains(className) {output.push(startNode)}; 

如果我这样做,它会运行。

if (startNode.classList.contains(className) {output.push(startNode)}; 
    missing a closing parentheses here ---^ 

More about the API itself here:如果你不写语法错误

if (('' + startNode.classList + '').indexOf(className) > -1) { 
    output.push(startNode) 
} 
+3

您的顶部代码缺少')'的if语句,你应该在你的控制台日志 –

+2

看到一个语法错误,了解如何使用控制台来检查脚本错误... *给一个人一条鱼,或教他钓鱼* – charlietfl

+0

['classList'](https://dom.spec.whatwg.org/#dom-element-classlist)返回['DOMTokenList'](https://dom.spec.whatwg.org /#domtokenlist)。 – Oriol

回答

-1

你的代码工作正常。

切记:classListwon't work in IE9-

+0

谢谢尼尔斯,我浪费了2〜5个小时,想知道为什么它不起作用。 :) –

+1

现在只是想知道谁是这样的:/ –