簡體   English   中英

為什么“else”在我的 if 語句代碼中不起作用?

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

只是想讓 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>

您必須再單擊一次才能開始,因為沒有人設置元素的 style.opacity。 是的,樣式表中有一個不透明度設置,但這並沒有在樣式屬性中設置任何內容。

所以改變測試,所以第一次(當 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