我相对较新,但是我找到了一个程序,用于弹出一个使用图像的模态/对话框。
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
#myImg {
border-radius: 5px;
cursor: pointer;
transition: 0.3s;
}
#myImg:hover {opacity: 0.7;}
.modal {
display: none;
position: fixed;
z-index: 1;
padding-top: 100px;
left: 0;
right: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba (0,0,0,0.7);
}
.modal-content {
margin: auto;
display: block;
width 80%;
max-width: 80%;
}
#caption {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
text-align: center;
color: #CCC;
padding: 10px 0;
height: 150px;
}
.modal-content, #caption {
-webkit-animation-name: zoom;
-webkit-animation-duration: 0.6s;
animation-name: zoom;
animation-duration: 0.6s;
}
@-webkit-keyframes zoom {
from {-webkit-transform:scale(0)}
to {-webkit-transform:scale(1)}
}
.close {
position: absolute;
top: 15px;
right: 35px;
color: #f1f1f1;
font-size: 40px;
font-weight: bold;
transition: 0.3s;
}
.close:hover, .close:focus {
color: #BBB;
text-decoration: none;
cursor: pointer;
}
</style>
</head>
<body>
<img src='https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded' id="myImg" alt="Floor 1" width="300" height="200">
<div id="myModal" class="modal">
<span class="close" onclick="document.getElementById('myModal').style.display='none'">×</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>
这是它的代码,它完美无缺。我关心的是特定的片段:
img.onclick = function() {
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}
我试图在嵌套在span标签内的图像上使用此onclick功能。有没有办法在上面粘贴的函数中使用:nth-child选择器?理想情况下,程序将在单击跨度时执行,并且嵌套图像将被执行。 &#39; SRC&#39;将在这里分配:
<img class="modal-content" id="img01" >
答案 0 :(得分:0)
目前还不清楚你的意思是:nth-child选择器,但通常的过程就是你首先得到你想要复制的元素源:
var source = document.getElementById('image').src
然后你将src
分配给另一个元素,如:
document.getElementById('image2').src = source;
现在关于如何选择第二个元素(要为其分配新src
的元素),它取决于它的确切位置。你能澄清一下吗?