gpt4 book ai didi

javascript - 需要打开两次的模态打开

转载 作者:行者123 更新时间:2023-11-28 00:42:01 25 4
gpt4 key购买 nike

在我的网站上,我的模式有问题。它应该由出现在嵌入标签附近的图标触发。虽然它可以激活模态,但它只显示模态内容而没有任何内容;关闭模式并再次单击 openModal() 函数后,将显示所需的嵌入文件。然后,当单击另一个文档时,它会显示前面的文档,直到关闭并再次单击。为什么会发生这种情况,我应该如何解决?

HTML(但只有一个嵌入):

<div class = "overlay-cont">
<embed src="link-to-pdf.pdf" >
<div class = "overlay">
<img class = "enlarge-icon" src = "arrow-icon.svg" onclick ="openModal(0)">
</div>
</div>

<div id = "myModal" class = "modal">
<span class="close cursor" onclick="closeModal()">&times;</span>
<div class="modal-content">
<embed id = "currentDoc">
</div>
</div>

JavaScript:

var doc = document.getElementsByTagName("EMBED");

function openModal(n){
document.getElementById("myModal").style.display = "block";
document.getElementById("currentDoc").src = doc[n].src;

}

function closeModal(){
document.getElementById("myModal").style.display = "none"
}

最佳答案

与其使用静态嵌入元素并为其赋予动态 src 属性值,不如在每次显示模态时生成动态嵌入元素,并在每次关闭模态时将其移除。

var doc = document.getElementsByTagName("EMBED");

function openModal(n) {
var modal = document.getElementById("myModal");
modal.style.display = "block";
var node = document.createElement("embed");
var src_attr = document.createAttribute("src");
src_attr.value = doc[n].src;
node.setAttributeNode(src_attr);
modal.appendChild(node);

}

function closeModal() {
var e = document.querySelectorAll("embed")[1];
e.parentNode.removeChild(e);
document.getElementById("myModal").style.display = "none"
}
<html>

<head>
</head>

<body>
<div class="overlay-cont">
<embed src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<div class="overlay">
<img class="enlarge-icon" src="https://cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico?v=4f32ecc8f43d" onclick="openModal(0)">
</div>
</div>

<div id="myModal" class="modal">
<span class="close cursor" onclick="closeModal()">&times;</span>
<div class="modal-content"></div>
</div>
</body>

</html>

关于javascript - 需要打开两次的模态打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52909937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com