javascript - 使用 for 和 if 语句隐藏/显示多个图像的迭代函数

标签 javascript html css for-loop if-statement

我想隐藏和显示多个图像,具体取决于“ShowPicture”函数内的输入代码“位置”。请参阅下面我的完整示例代码:

    <!DOCTYPE HTML>
<html>
<head>
<title>title</title>
<style>
body {font-size: 30px; text-align: center; vertical-align: middle}
#containertop {background-color: #808080; width: 100%; position:right}
#parent {display:flex;}
#containercircle {width: 30%;}
#containerimg {background-color: rgba(50,0,50,1);flex:1;}
header, footer {padding: 1em; color: rgba(50,0,50,1);background-color: #808080;clear: left;text-align: center}
#circle {width:100px;float:left;margin:5px; overflow:hidden; height:100px; -webkit-border-radius:50%; -moz-border-radius:50%; border-radius:50%; background-color:rgba(50,0,50,1); font-size:15px; line-height:100px}    #circle:hover{font-size:16.5px;margin:5px;transition: All 0.2s ease-in-out}
.crop {width:40px;height:40px;}
</style>
<script>function ShowPicture(tekst, places){
     for (var i = 0, i < places.length; i++) {
        var idCust ="pic" + places[i];
        var imag = document.getElementById(idCust);
     if(imag.style.display === 'block'){ 
         imag.style.display = 'none'; tekst.style.color = '';} 
     else { 
         imag.style.display = 'block'; tekst.style.color = 'red';}}}
</script>
</head>
<body>
<header><h1><div id="containertop">header</div></h1></header>
<div id="parent">
<div id ="containercircle" float = "right">
<div id="circle" onclick="ShowPicture(this,1)">Test</div>
</div>
<div id ="containerimg" >
<img id="pic0" class ="crop" src="boor.jpg" style="display:none;"/>
<img id="pic1" class ="crop" src="boor.jpg" style="display:none;"/>
<img id="pic2" class ="crop" src="boor.jpg" style="display:none;"/>
<img id="pic3" class ="crop" src="boor.jpg" style="display:none;"/>
</div>
</div>
<footer><div id="containertop">footer</div></footer>
</body>
</html>

“地点”代码的两个示例是

"123" and "24"

该函数应分别读取三个数字,并在 css 部分隐藏或显示相应的“idCust”。我在 css 中创建了名为“pic1”、“pic2”等的 id。

出于某种原因,当我使用“onclick”语句单击 div 后调用“ShowPicture”函数时,这不起作用。我感觉它与我的 for 循环有关..也许有一种方法可以同时隐藏/显示所有内容,但它应该只显示与我单击的 div 相对应的图像。

最佳答案

您的问题是您的循环中有一个错误,即 for (var i = 0, i < places.length; i++) 中的逗号。 , 应替换为分号。

您在 string 上循环的另一件事你通过了1这是 Number所以不会有任何循环,你需要将它作为 string 传递在:

onclick="ShowPicture(this,'1')"

演示:

function ShowPicture(tekst, places) {
  for (var i = 0; i < places.length; i++) {
    var idCust = "pic" + places[i];
    var imag = document.getElementById(idCust);
    if (imag) {
      if (imag.style.display === 'block') {
        imag.style.display = 'none';
        tekst.style.color = '';
      } else {
        imag.style.display = 'block';
        tekst.style.color = 'red';
      }
    }
  }
}
body {
  font-size: 30px;
  text-align: center;
  vertical-align: middle
}

#containertop {
  background-color: #808080;
  width: 100%;
  position: right
}

#parent {
  display: flex;
}

#containercircle {
  width: 30%;
}

#containerimg {
  background-color: rgba(50, 0, 50, 1);
  flex: 1;
}

header,
footer {
  padding: 1em;
  color: rgba(50, 0, 50, 1);
  background-color: #808080;
  clear: left;
  text-align: center
}

#circle {
  width: 100px;
  float: left;
  margin: 5px;
  overflow: hidden;
  height: 100px;
  -webkit-border-radius: 50%;
  -moz-border-radius: 50%;
  border-radius: 50%;
  background-color: rgba(50, 0, 50, 1);
  font-size: 15px;
  line-height: 100px
}

#circle:hover {
  font-size: 16.5px;
  margin: 5px;
  transition: All 0.2s ease-in-out
}

.crop {
  width: 40px;
  height: 40px;
}
<header>
  <h1>
    <div id="containertop">header</div>
  </h1>
</header>
<div id="parent">
  <div id="containercircle" float="right">
    <div id="circle" onclick="ShowPicture(this,'1')">Test</div>
  </div>
  <div id="containerimg">
    <img id="pic0" class="crop" src="boor.jpg" style="display:none;" />
    <img id="pic1" class="crop" src="boor.jpg" style="display:none;" />
    <img id="pic2" class="crop" src="boor.jpg" style="display:none;" />
    <img id="pic3" class="crop" src="boor.jpg" style="display:none;" />
  </div>
</div>

关于javascript - 使用 for 和 if 语句隐藏/显示多个图像的迭代函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46580302/

相关文章:

javascript - 使用 jQuery 添加类仅适用于第一个元素

css - IE7/8 中表行之间的空格

javascript - 显示和加载一次

javascript - 鼠标悬停时暂停打开 div

javascript - 如何跨应用更新保存用户数据?

javascript - 更改背景图像

javascript - Knockout.js 可以更改不在 viewModel 中的变量吗

javascript - 函数式编程中连接等线程资源的处理方法

javascript - 如何在Mapbox中添加点?

html - position:fixed - 和其他元素