简体   繁体   English

模态函数不显示模态

[英]modal function not showing the modal

Hi im writing a function that i can reuse to show/hide modals and on my website but it does not seem to work. 嗨,我正在编写一个可以重用以显示/隐藏模式以及在我的网站上使用的函数,但它似乎不起作用。 when i used the same code outside a funtion and using direct id and class names instead of variables it worked fine its only when im passing them in that it does not appear to work? 当我在函数外使用相同的代码并使用直接的id和类名而不是变量时,它仅在通过它们时会正常工作,因为它似乎不起作用?

<input id="lrBtn" type="button" value="Login/Register" onclick="ShowModal('PopUpLR','closeLR');" />




function ShowModal(var popup,var closeBtn){
        var modal = document.getElementById(popup);
        var span = document.getElementsByClassName(closeBtn)[0];
        modal.style.display = "block";
        span.onclick = function() {
            modal.style.display = "none";
        }
        window.onclick = function(event) {
            if (event.target == modal) {
                modal.style.display = "none";
            }
        }
    }

The way you define your ShowModal function is wrong You need to write it like this 您定义ShowModal函数的方式是错误的您需要这样编写

function ShowModal(popup,closeBtn){
 // your code
}

instead of 代替

function ShowModal(var popup,var closeBtn)
{
// your code
}

Full code: 完整代码:

function ShowModal(popup,closeBtn){
        var modal = document.getElementById(popup);
        var span = document.getElementsByClassName(closeBtn)[0];
        modal.style.display = "block";
        span.onclick = function() {
            modal.style.display = "none";
        }
        window.onclick = function(event) {
            if (event.target == modal) {
                modal.style.display = "none";
            }
        }
    }

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

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