简体   繁体   English

为什么“else”在我的 if 语句代码中不起作用?

[英]Why "else" doesn't work in my if statement code?

Simply want the else to work and apply the original opacity which is 1, but why isn't it working?只是想让 else 工作并应用原始不透明度为 1,但为什么它不起作用?

 let container = document.getElementById("container").children; let contArr = [...container]; contArr.forEach( item => { item.addEventListener("click", function handleClick() { if (item.style.opacity = "1") { item.style.opacity = "0.5"; } else { item.style.opacity = "1"; } })});
 .container { line-height: 0; column-count: 3; column-gap: 0; column-fill: balance; }.container img { width: 100%; opacity: 1; }.container img:hover { scale: 1.1; transition: 0.5s; left: 120%; cursor: pointer; }.viewer { visibility: hidden; display: flex; justify-content: center; align-items: center; position: fixed; top: 0; height: 100%; width: 100%; background: rgb(255, 0, 0, 0.5); /* transition: 1s; */ }.closeViewer { color: #000; background: rgb(255, 255, 255, 0.5); position: absolute; top: 100px; right: 100px; font-family: Arial, Helvetica, sans-serif; font-size: 3rem; padding: 0 10px; cursor: pointer; }
 <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="container" id="container"> <img class="img"src="https://images.freeimages.com/images/large-previews/a31/colorful-umbrella-1176220.jpg"> <img class="img"src="https://images.freeimages.com/images/large-previews/9e6/abstract-polygonal-low-poly-photos-texture-3-636190.jpg"> <img class="img"src="https://images.freeimages.com/images/large-previews/aa8/evening-01-1055813.jpg"> <img class="img"src="https://live.staticflickr.com/3437/3403778548_15f48ab99e_b.jpg"> <img class="img"src="https://cdn.pixabay.com/photo/2017/07/25/01/22/cat-2536662_960_720.jpg"> <img class="img"src="https://cdn.pixabay.com/photo/2018/08/01/19/35/wolf-3577956_960_720.jpg"> <img class="img"src="https://live.staticflickr.com/3437/3403778548_15f48ab99e_b.jpg"> <img class="img"src="https://cdn.pixabay.com/photo/2017/07/25/01/22/cat-2536662_960_720.jpg"> <img class="img"src="https://cdn.pixabay.com/photo/2018/08/01/19/35/wolf-3577956_960_720.jpg"> <img class="img"src="https://images.freeimages.com/images/large-previews/a31/colorful-umbrella-1176220.jpg"> <img class="img"src="https://images.freeimages.com/images/large-previews/9e6/abstract-polygonal-low-poly-photos-texture-3-636190.jpg"> <img class="img"src="https://images.freeimages.com/images/large-previews/aa8/evening-01-1055813.jpg"> </div> <div class="viewer" id="viewer"> <button class="closeViewer" id="closeViewer" onclick="closeViewer()">X</button> </div> <script src="./js.js"></script> </body> </html>

You have to do one more click to start with because noone has set the style.opacity of the element.您必须再单击一次才能开始,因为没有人设置元素的 style.opacity。 Yes, there is a setting of opacity in the style sheet but this hasn't set anything in the style attribute.是的,样式表中有一个不透明度设置,但这并没有在样式属性中设置任何内容。

So change the test so the first time (when the style.opacity doesn't have either 1 or 0.5 as a value) you set it:所以改变测试,所以第一次(当 style.opacity 没有 1 或 0.5 作为值时)你设置它:

 if (item.style.opacity.= "0.5") { item.style.opacity = "0;5". } else { item.style;opacity = "1" }

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

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