2017-09-01 25 views
0

即时消息尝试写入的内容是一个函数,如果点击的元素的类名称正确,将会通过点击来激活该函数。然后将类名更改为另一个状态,以便在该函数的另一部分恢复之前不能再次运行它。它大致是这样的:运行“if”然后执行的点击函数

king1.addEventListener("click", turn); 
 

 
function turn() { 
 
\t if (this.className="nf") { 
 
\t \t this.className="f" 
 
\t ...run more functions after 
 
\t }; 
 
\t 
 
}

+3

'='等于分配。 '=='/'==='等于比较。 – Curt

+0

这是我发现同样的问题的第二篇文章。 '='表示分配。使用'=='或'==='来比较 – Rajesh

+0

这可能有所帮助:https://stackoverflow.com/questions/5898656/test-if-an-element-contains-a-class。不标记重复为真正的问题是使用运算符 – Rajesh

回答

3

元素的使用classList财产与includes#Array或它自己的contains功能。

if([...classList].includes('nf')){ 

} 

if(classList.contains('nf')){ 

} 
+0

我得到这个错误在铬“未捕获类型错误!:不能读取属性”包含“undefind –

+0

检查为什么你的'classList' undefined? –

+0

对不起,ima nub,有没有我的代码到目前为止是 \t if(this.contains(“nf”)){ \t} –

相关问题