简体   繁体   English

无法关闭Java语言中的模式

[英]Unable to close modal in Javascript

I'm writing some js code in my ebook project. 我在电子书项目中写了一些js代码。 The problem is in a modal, used to show, like a popup, reference from others pages. 问题出在模态中,用来像弹出窗口一样显示其他页面的引用。 Here is the part of my project dedicated to close the modal. 这是我的项目的一部分,专门用于关闭模式。

    // Riferimento al Modal
var modal = document.getElementById('modalversetti');
// Riferimento allo <span> che chiude il Modal
var span = document.getElementsByClassName("close")[0];
// Quando l'utente preme sullo <span> (x), si chiude il Modal
span.onclick = function() {
modal.style.display = "none";
}

And here (better) all the code (js+html+css) showing the modal running but unable to close. 这里(更好)的所有代码(js + html + css)显示了模态运行但无法关闭。 https://jsfiddle.net/emanuele_tinari/gkL5sh3x/ Modal built via js run very well, but, unfortunately, close button seems don't. https://jsfiddle.net/emanuele_tinari/gkL5sh3x/通过js构建的模式运行得很好,但是不幸的是,关闭按钮似乎没有。 thanks. 谢谢。

Your code tries to find the close span to attach the listener immediately when the page loads. 您的代码试图找到close范围,以便在页面加载时立即附加侦听器。 You haven't created the modal at this point, so it fails. 您尚未创建模态,因此它失败了。

If you cut the chunk of code in your question and paste it immediately at the end of the function that creates your modal, the close button will work. 如果您剪切问题中的代码块并将其立即粘贴到创建模态的函数的末尾,则关闭按钮将起作用。

This will do the work : 这将完成工作:

  document.onclick = function(event) {
    var el = event.target;
    if (el.className == "close") {
    // Riferimento al Modal
    var modal = document.getElementById('modalversetti');

        modal.style.display = "none";
    }
};

but you have more problems with your code ... 但是您的代码有更多问题...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM