简体   繁体   English

Javascript 等于在 if 语句中突出显示

[英]Javascript Equals to gets highlighted in if statement

So i was making a pretty simple carousel in which when a user clicks on a button, the image or content changes.所以我制作了一个非常简单的旋转木马,当用户点击一个按钮时,图像或内容会发生变化。 I got it to work halfway but i am only one problem.我让它工作了一半,但我只是一个问题。 In the code i first check if the display of card1 is block and the display of card3 is none.在代码中,我首先检查 card1 的显示是否为块,card3 的显示是否为无。 That is where it shows the problem.这就是它显示问题的地方。 In the first part it is fine but after the && when i write card3.style = "display:none" it highlights the equals to sign and doesn't work.在第一部分很好,但是在 && 之后,当我写card3.style = "display:none"它突出显示等于符号并且不起作用。 I have tried changing the orientation of the code and even adding == but to no avail.我试过改变代码的方向,甚至添加==但无济于事。 Can someone help me with this?有人可以帮我弄这个吗?

You can checkout my code:你可以检查我的代码:

   <div class="card1" id="card1">
        
    </div>

    <div class="card2" id="card2">
        
    </div>

    <div class="card3" id="card3">
        
    </div>

    <button onclick="change()">Change</button>
.card1{
    width: 40%;
    height: 40%;
    background-color: blue;
}
.card2{
    width: 40%;
    height: 40%;
    background-color: red;
    display: none;
}
.card3{
    display: none;
    width: 40%;
    height: 40%;
    background-color: green;
}
var card1 = document.getElementById("card1")
var card2 = document.getElementById("card2")
var card3 = document.getElementById("card3")

function change(){
    if(card1.style = 'display:block;' && card3.style = 'display:none'){
        card1.style = 'display:none';
        card2.style = 'display:block';
    
    }
    

}

Use == to compare:使用==进行比较:

var card1 = document.getElementById("card1")
var card2 = document.getElementById("card2")
var card3 = document.getElementById("card3")

function change(){
    if(card1.style.display == 'block' && card3.style.display == 'none'){
        card1.style = 'display:none';
        card2.style = 'display:block';
    
    }
}

Use == operator and update card1.style to card1.style.display .使用==运算符并将card1.style更新为card1.style.display

 var card1 = document.getElementById("card1"); var card2 = document.getElementById("card2"); var card3 = document.getElementById("card3"); function change() { if (card1.style.display == 'block' && card3.style.display == 'none') { card1.style = 'display:none'; card2.style = 'display:block'; } }
 .card1 { width: 40%; height: 40%; background-color: blue; }.card2 { width: 40%; height: 40%; background-color: red; }.card3 { display: none; width: 40%; height: 40%; background-color: green; }
 <div class="card1" id="card1" style="display: block;"> Card 1 </div> <div class="card2" id="card2" style="display: none;"> Card 2 </div> <div class="card3" id="card3" style="display: none;"> Card 3 </div> <button onclick="change()">Change</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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