简体   繁体   English

弹出窗口内关闭按钮不解析(jQuery on click event)

[英]Close button not ansering(jQuery on click event) inside popup

I have a problem that seems to be simple but I can't find what's going on. 我有一个看似简单的问题,但我找不到正在发生的事情。 I have a popup with onClick , inside menu popup box is showing. 我有一个带有onClick弹出窗口,正在显示菜单弹出框。 I have element on my page that is called popup, and it grows to fill the screen on click by adding popupActive class and than using load() , it then loads some content from the server. 我的页面上有一个名为popup的元素,通过添加popupActive类并比使用load()来填充点击时的整个屏幕,然后从服务器加载一些内容。 It all works great so far. 到目前为止一切都很好。 Then, whan a user clicks on X it should remove popupActive class and shrink back to original state (this is accomplished with scale(0) and scale(1) ). 然后,当用户单击X时,它应该删除popupActive类并缩小到原始状态(这可以通过scale(0)scale(1) )。 But instead when I click X nothing happenes, console doesn't show any errors, and I tried console.log("Hello!") to see if it ever got there (script), but it doesn't show anything. 但是,当我单击X时,什么也没有发生,控制台没有显示任何错误,我尝试使用console.log("Hello!")来查看它是否到达那里(脚本),但是什么也没显示。

My code is below. 我的代码如下。

HTML HTML

<section id="popup" class="popup"><button class="about-close">X</button</section>

CSS CSS

.popup {
    position: fixed;
    top: 0px;
    left: 0px;
    width: 100vw;
    min-height: 100vh;
    border-radius: 50px;
    z-index: 0;
    transform: scale(0);
    transition: 1s linear all;
}
.popupActive {
    transform: scale(1);
    z-index: 99;
    transition: 1s linear all;
}
.about-close {
    position: fixed;
    top: 30px;
    right: 30px;
    width: 50px;
    height: 50px;
    background: transparent;
    display: block;
    border-radius: 50%;
    border: 1px solid #f74d4e;
    color: #f74d4e;
    font-size: 30px;
    cursor: pointer;
    z-index: 999;
}

JavaScript JavaScript的

$(".li-a").on('click',function(event) {
    event.preventDefault();
    $("#popup").addClass("popupActive").load("Ajax/about.html");
});
$(".about-close").on('click',function(event) {
    event.preventDefault();
    console.log("Hello!");
    $(".about-container").remove();
    $("#popup").removeClass("popupActive");
});
   <section id="popup" class="popup"><button class="about-close">X</button>
    <div class="popup-content"></div>
   </section>

script: 脚本:

$(".li-a").on('click',function(event) {
        event.preventDefault();

        $("#popup").addClass("popupActive");
         $("#popup .popup-content").load("Ajax/about.html");
    });
    $(".about-close").on('click',function(event) {
        event.preventDefault();
        console.log("Hello!");
        $("#popup .popup-content").remove();
        $("#popup").removeClass("popupActive");
    });

Your load method is asynchronous - that means it may not be finished adding the internal html before you try to attach the click-handler that closes the modal. 您的load方法是异步的-这意味着在尝试附加关闭模式的点击处理程序之前,可能无法完成内部html的添加。

You could attach the click handler in the callback for load (like .load("Ajax/about.html", function() { /* attach it here */ }) ), but I think a better solution would be to attach the listener higher up, just once even before the modal loads: 您可以在点击回调中附加点击处理程序以进行加载(例如.load("Ajax/about.html", function() { /* attach it here */ }) ),但是我认为更好的解决方案是附加侦听器更高,甚至在模式加载之前一次:

$("#popup").on('click', '.about-close',function(event) {
    event.preventDefault();
    console.log("Hello!");
    $(".about-container").remove();
    $("#popup").removeClass("popupActive");
});

That code just needs to run once when the initial page DOM is ready. 当初始页面DOM准备就绪时,该代码只需运行一次。

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

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