javascript - 在同一段落中包含两个链接,它们在模态窗口中启动不同的图像

标签 javascript html css modal-dialog

我试图让同一段落中的不同链接在模态窗口中启动不同的图像,但是当您单击第二个时,它只会再次显示第一个图像。关于如何纠正它的任何建议?我试图找到一种解决方案,不涉及将每个模式分离到它自己的 div 中(我需要强制每个 div 不换行)。我想知道是否有更优雅的解决方案......谢谢!这是我的 javascript、css 和 html。

var modal = document.getElementById('myModal');

modal.addEventListener('click',function(){
this.style.display="none";
})

var span = document.getElementsByClassName("close")[0];

span.onclick = function() {
  modal.style.display = "none";
}

function launchModal(element) {;
  var modalImg = document.getElementById("img01");
  modalImg.src = element.parentElement.children[1].src;
  modalImg.alt = this.alt;
  modal.style.display = "block";
}
img.responsive {
display:none;
  }

/* The Modal (background) */

.modal {
  display: none;
  /* Hidden by default */
  position: fixed;
  /* Stay in place */
  z-index: 2000;
  /* Sit on top */
  padding-top: 100px;
  /* Location of the box */
  left: 0;
  top: 0;
  width: 100%;
  /* Full width */
  height: 100%;
  /* Full height */
  overflow: auto;
  /* Enable scroll if needed */
  background-color: rgba(0, 0, 0, 0.75);
  /* Black w/ opacity */
}


/* Modal Content (image) */

.modal-content {
  margin: auto;
  display: block;
  max-width: 70%;
  max-height: 70%;
}

/* Add Animation */

.modal-content,
#caption {
  -webkit-animation-name: zoom;
  -webkit-animation-duration: 0.6s;
  animation-name: zoom;
  animation-duration: 0.6s;
}

@-webkit-keyframes zoom {
  from {
    transform: scale(0)
  }
  to {
    transform: scale(1)
  }
}

@keyframes zoom {
  from {
    transform: scale(0.1)
  }
  to {
    transform: scale(1)
  }
}


/* The Close Button */

.close {
  font-family: Arial;
  position: absolute;
  top: 75px;
  right: 35px;
  color: white;
  font-size: 60px;
  transition: 0.3s;
}

.close:hover,
.close:focus {
  color: white;
  text-decoration: none;
  cursor: pointer;
}
<p>
Archie is a dog that likes to steal things.  If you are interested in seeing Archie, click on <a onclick="launchModal(this)" href="#">this link</a>
  <img class="responsive" src="https://i.imgur.com/cQy7Yg.jpg">and a picture of him will pop up.
Now we have a new sentence with a different modal in the same paragraph.  Wellington is a dog that likes to smile and eat cupcakes.  If you are interested in seeing Wellington, click on <a onclick="launchModal(this)" href="#">this link</a>
  <img class="responsive" src="https://i.imgur.com/sPhLIKY.jpg">and a picture of him will pop up.</p>

<!-- The Modal -->
<div id="myModal" class="modal">
  <span class="close">x</span>
  <img class="modal-content" id="img01">
</div>

最佳答案

问题在于这两个元素具有相同的父元素,因此您将始终使用 parentElement.children[1] 选择相同的图像。一个想法是将图像链接作为数据属性包含在链接自身并避免额外的元素。

var modal = document.getElementById('myModal');

modal.addEventListener('click',function(){
this.style.display="none";
})

var span = document.getElementsByClassName("close")[0];

span.onclick = function() {
  modal.style.display = "none";
}

function launchModal(element) {
  var modalImg = document.getElementById("img01");
  modalImg.src = element.getAttribute('data-src');
  modalImg.alt = element.getAttribute('data-alt');
  modal.style.display = "block";
}
div.lisa{
display:inline;
}

/* The Modal (background) */

.modal {
  display: none;
  /* Hidden by default */
  position: fixed;
  /* Stay in place */
  z-index: 2000;
  /* Sit on top */
  padding-top: 100px;
  /* Location of the box */
  left: 0;
  top: 0;
  width: 100%;
  /* Full width */
  height: 100%;
  /* Full height */
  overflow: auto;
  /* Enable scroll if needed */
  background-color: rgba(0, 0, 0, 0.75);
  /* Black w/ opacity */
}


/* Modal Content (image) */

.modal-content {
  margin: auto;
  display: block;
  max-width: 70%;
  max-height: 70%;
}

/* Add Animation */

.modal-content,
#caption {
  -webkit-animation-name: zoom;
  -webkit-animation-duration: 0.6s;
  animation-name: zoom;
  animation-duration: 0.6s;
}

@-webkit-keyframes zoom {
  from {
    transform: scale(0)
  }
  to {
    transform: scale(1)
  }
}

@keyframes zoom {
  from {
    transform: scale(0.1)
  }
  to {
    transform: scale(1)
  }
}


/* The Close Button */

.close {
  font-family: Arial;
  position: absolute;
  top: 75px;
  right: 35px;
  color: white;
  font-size: 60px;
  transition: 0.3s;
}

.close:hover,
.close:focus {
  color: white;
  text-decoration: none;
  cursor: pointer;
}
<p>
Archie is a dog that likes to steal things.  If you are interested in seeing Archie, click on <a onclick="launchModal(this);return false;" data-alt="img1" data-src="https://i.imgur.com/cQy7Yg.jpg" href="#">this link</a> and a picture of him will pop up.
Wellington is a dog that likes to smile and eat cupcakes.  If you are interested in seeing Wellington, click on <a onclick="launchModal(this);return false;" data-alt="img2" data-src="https://i.imgur.com/sPhLIKY.jpg" href="#">this link</a>
 and a picture of him will pop up.</p>

<!-- The Modal -->
<div id="myModal" class="modal">
  <span class="close">x</span>
  <img class="modal-content" id="img01">
</div>

如果你想保留 img 元素,你需要在它们和它们各自的 a 标签之间创建一种关系。这是我使用 ID 的示例:

var modal = document.getElementById('myModal');

modal.addEventListener('click',function(){
this.style.display="none";
})

var span = document.getElementsByClassName("close")[0];

span.onclick = function() {
  modal.style.display = "none";
}

function launchModal(element) {;
  var modalImg = document.getElementById("img01");
  modalImg.src = document.getElementById(element).src;
  modalImg.alt = document.getElementById(element).alt;
  modal.style.display = "block";
}
div.lisa{
display:inline;
}

/* The Modal (background) */
img.responsive {
 display:none;
}
.modal {
  display: none;
  /* Hidden by default */
  position: fixed;
  /* Stay in place */
  z-index: 2000;
  /* Sit on top */
  padding-top: 100px;
  /* Location of the box */
  left: 0;
  top: 0;
  width: 100%;
  /* Full width */
  height: 100%;
  /* Full height */
  overflow: auto;
  /* Enable scroll if needed */
  background-color: rgba(0, 0, 0, 0.75);
  /* Black w/ opacity */
}


/* Modal Content (image) */

.modal-content {
  margin: auto;
  display: block;
  max-width: 70%;
  max-height: 70%;
}

/* Add Animation */

.modal-content,
#caption {
  -webkit-animation-name: zoom;
  -webkit-animation-duration: 0.6s;
  animation-name: zoom;
  animation-duration: 0.6s;
}

@-webkit-keyframes zoom {
  from {
    transform: scale(0)
  }
  to {
    transform: scale(1)
  }
}

@keyframes zoom {
  from {
    transform: scale(0.1)
  }
  to {
    transform: scale(1)
  }
}


/* The Close Button */

.close {
  font-family: Arial;
  position: absolute;
  top: 75px;
  right: 35px;
  color: white;
  font-size: 60px;
  transition: 0.3s;
}

.close:hover,
.close:focus {
  color: white;
  text-decoration: none;
  cursor: pointer;
}
<p>
Archie is a dog that likes to steal things.  If you are interested in seeing Archie, click on <a onclick="launchModal('img1');return false;"  href="#">this link</a><img class="responsive" src="https://i.imgur.com/cQy7Yg.jpg" alt="img1" id="img1"> and a picture of him will pop up.
Wellington is a dog that likes to smile and eat cupcakes.  If you are interested in seeing Wellington, click on <a onclick="launchModal('img2');return false;"  href="#">this link</a><img class="responsive" src="https://i.imgur.com/sPhLIKY.jpg" alt="img2" id="img2">
 and a picture of him will pop up.</p>

<!-- The Modal -->
<div id="myModal" class="modal">
  <span class="close">x</span>
  <img class="modal-content" id="img01">
</div>

关于javascript - 在同一段落中包含两个链接,它们在模态窗口中启动不同的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51548567/

相关文章:

javascript - 我如何从插槽子定义与 vue 2.x 中的包含组件进行通信?

javascript - 无法访问从构造函数创建的对象

javascript - 我们如何动态传递图形类型

JavaScript 打印带有 20 位小数的错误数字

html - Bootstrap 轮播定制

javascript - gif 动画完成后如何激活页面滚动

javascript - 使用 CSS 避免水平滚动以将绝对 div 置于右侧

javascript - 显示/隐藏 HTML 表单的表格或 div

html - 为什么 div 可以正确拉伸(stretch),但图像却不能?

html - css 阿拉伯语 ttf 字体不起作用