为什么!=工作而只是=不?

这个问题在这里已经有了答案:

  • 应该在JavaScript比较中使用哪个等于运算符(== vs ===)? 50个答案

  • 你的条件实际上是一项任务:

    if (document.getElementById("hiddenButton").style.visibility = "hidden") {
    

    你应该使用==

    if (document.getElementById("hiddenButton").style.visibility == "hidden") {
    

    =是一个赋值操作。

    !=是一个不等式运算符。

    ==是一个相等运算符。

    我猜你需要的是==运算符。 所以用你的代码替换:

    if (document.getElementById("hiddenButton").style.visibility == "hidden") {
    

    JS比较运算符

    ==      is equal to 
    ===     is exactly equal to (value and type)
    !=      is not equal
    

    例如:

    var x = 1;  //define and assigned and now x equal to 1
    x = 3;        //now x equal to 3
    
    if( x == 4) {
        //you won't see this alert
        alert('Hello, x is 4 now');
    } else {
        //you will see this alert
        alert('Hello, x hasn not been changed and it is still ' + x.toString());
    }
    
    链接地址: http://www.djcxy.com/p/3253.html

    上一篇: Why does != work and just = doesn't?

    下一篇: What is the difference between == and === in JavaScript?