简体   繁体   中英

function doesn't work

I need a little help with this function:

function  passar() {  
    var fotos1 = document.getElementById("foto1");
    var fotos2 = document.getElementById("foto2");
    var fotos3 = document.getElementById("foto3");

    if ( fotos1.style.display = "block" ) {   
        fotos1.style.display = "none";  
        fotos2.style.display = "block";  
    } else if ( fotos2.style.display = "block" ) {  
        fotos2.style.display = "none";  
    }  
} 

It was supposed to hide "fotos 2" when the first condition is changed but it didn't work, someone knows why?

= for assignment

== for comparison

=== for strict comparison

if (fotos1.style.display === "block") {   
fotos1.style.display = "none";  
fotos2.style.display = "block";  
 } else if (fotos1.style.display === "none") {  
 fotos2.style.display = "none";    
  }  
} 

Your conditional statement is doing an assignment rather than a comparison:

if (fotos1.style.display === "block") {   
    fotos1.style.display = "none";  
    fotos2.style.display = "block";  
} else if (fotos1.style.display === "none") {  
    fotos2.style.display = "block";    
}

Also, it is recommended to use strict comparison using === over the weak comparison == . You get the added benefit of the comparison also being roughly 4X faster in some browsers.

You should be using a comparison operator which is double equals in this case:

if (fotos1.style.display == "block") {

Instead you are using a single equals which is an assignment parameter:

if (fotos1.style.display = "block") {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM