Why does != work and just = doesn't?

This question already has an answer here:

  • Which equals operator (== vs ===) should be used in JavaScript comparisons? 50 answers

  • Your condition is actually an assignment:

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

    You should be using == :

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

    The = is an assignment operation.

    The != is an inequality operator.

    The == is an equality operator.

    I guess what you need is the == operator. So replace your code with:

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

    JS Comparison operators

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

    For example:

    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/3254.html

    上一篇: 何时使用===操作符检查JavaScript?

    下一篇: 为什么!=工作而只是=不?