如何通过单击HTML按钮隐藏和显示图像?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (135)

当我点击任何字母按钮时,我都试图按照字母字符来改变图像。

HTML

<div id="btn">
  <input type="button" onclick="hideImage();" value="A" class="btn-group" />
</div>

<div id="images">
  <img src="Letter-A.png" class="image-group"></img>
</div>

JavaScript

 function hideImage() {
   var button = document.getElementsByClassName("btn-group");
   document.getElementsByClassName('image-group').onclick = function() {
       if (button.style.display == "none") {
         button.style.display = "block";
       } else {
         button.style.display = "none";
       }

我得到一个错误的“显示”属性不能设置为未定义。

提问于
用户回答回答于

我想你有一个按钮和图像的列表,其中唯一的区别是字母。在这种情况下,你可以这样做。

let btns = document.getElementsByClassName('btn-action')
for(let i = 0; i < btns.length; i++) {
  btns[i].addEventListener("click", function() {
    hideImage(btns[i].value);
  })
}

function hideImage(letter){
  let id = "image-" + letter;
  let img = document.getElementById(id)
  if(img.style.display === "none") {
    img.style.display = "block";
  }
  else {
    img.style.display = "none";
  }
}
<div id="btn">
  <input type="button" value="A" class="btn-group btn-action"/>
 </div>

 <div id="images">
    <img src="Letter-A.png" id="image-A" class="image-group">
 </div>

请避免使用内联HTML事件属性

用户回答回答于
/* Collect all .btn classes into a NodeList and comvert it into 
|| an array
*/
var btnGroup = Array.from(document.getElementsByClassName("btn"));

/* Collect all .img classes into a NodeList and convert it into 
|| an array
*/
var imgGroup = Array.from(document.getElementsByClassName('img'));

/* Iterate (loop) thru the imgGroup array with forEach() array
|| method.
*/// In each loop get a .btn from the btnGroup array's index 
//// position that corresponds with the current index of the loop.
//// Register an onclick event handler that toggles the .off class
//// to a .img of the imgGroup array positioned at current loop 
//// index.


imgGroup.forEach(function(img, idx) {

  btnGroup[idx].onclick = function() {

    img.classList.toggle('off');
  }

});

.off {
  display: none;
}

<div id="btn-group">
  <input type="button" value="A" class="btn" />
  <input type="button" value="B" class="btn" />
  <input type="button" value="C" class="btn" />
  <input type="button" value="D" class="btn" />
  <input type="button" value="E" class="btn" />
</div>

<div id="img-group">
  <img src="https://www.luckyclovertrading.com/images/LBL14CG_A_a.jpg" class="img" width='100'>

  <img src="https://etc.usf.edu/presentations/extras/letters/theme_alphabets/26/12/B-400.png" class="img" width='100'>

  <img src="https://etc.usf.edu/presentations/extras/letters/fridge_magnets/orange/13/C-400.png" class="img" width='100'>

  <img src="https://etc.usf.edu/presentations/extras/letters/varsity_letters/37/16/D-400.png" class="img" width='100'>

  <img src="https://images-na.ssl-images-amazon.com/images/I/31ETtJ6FG6L.jpg" class="img" width='100'>
</div>

扫码关注云+社区

领取腾讯云代金券