您好我正在尝试找到一种解决方案,当我点击旋转木马上的图像时出现模态。我为我的一张图片做了这个,但是我想对大约20张图像使用相同的代码。有没有办法在不复制每个图像的代码的情况下执行此操作?这是使用bootstrap,但我认为不需要能够做到这一点,谢谢。
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<title>Photography</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="css/styles.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<style>
</style>
<body>
<div class="container" style="padding-bottom:15px;">
<div class="row">
<div class="col-2"></div>
<div class="col-8">
<h2>Carousel Example</h2>
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner">
<div class="item active">
<img src="Photos/Buttons/BridgeNON.png" alt= "Bridge" class="img‐fluid" onmouseover="this.src='Photos/Buttons/Bridge.png'" onmouseout="this.src='Photos/Buttons/BridgeNON.png'" style="width:100%;">
</div>
<div class="item">
<img id="myImg" src="Photos/Buttons/waterNON.png" alt= "Water" class="img‐fluid" onmouseover="this.src='Photos/Buttons/water.png'" onmouseout="this.src='Photos/Buttons/waterNON.png'" style="width:100%;">
</div>
<div class="item">
<img src="" alt="" style="width:100%;">
</div>
</div>
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
<div class="col-2"></div>
</div>
</div>
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="img01">
<div id="caption"></div>
</div>
<script>
var modal = document.getElementById('myModal');
var img = document.getElementById('myImg');
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
img.onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}
var span = document.getElementsByClassName("close")[0];
span.onclick = function() {
modal.style.display = "none";
}
</script>
</body>
</html>
答案 0 :(得分:0)
用以下内容替换您的Javascript。这将获取#myCarousel
中的所有图像,循环遍历它们,并添加事件。
var modal = document.getElementById('myModal');
var imgs = document.getElementById('myCarousel').getElementsByTagName('img');
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
for (var i = 0; i < imgs.length; i++) {
imgs[i].onclick = function() {
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}
}
var span = document.getElementsByClassName("close")[0];
span.onclick = function() {
modal.style.display = "none";
}