简体   繁体   English

jQuery click事件未绑定到元素

[英]jQuery click event not binding to element

I am building a Chrome Extension for Instagram. 我正在为Instagram构建Chrome扩展程序。 My problem is occurring on the "single post" page of instagram (when you click on a post and it is shown as a modal). 我的问题出现在instagram的“单个帖子”页面上(当您单击帖子并显示为模式时)。

When the post modal is showing I append a custom modal/pop-up of my own to the page with this code: 当显示帖子模式时,我将使用以下代码将自己的自定义模式/弹出窗口附加到页面上:

function attachUpgradePopup(str) {
    $(".upgrade-popup-container").remove();
    $("body").append(`
        <div class="upgrade-popup-container">
            <div class="upgrade-popup">
                <img class="popup-img" src="https://i.imgur.com/6sOdwYs.png">
                <p class="upgrade-popup-text">This post is ${str}.</p><br>
                <p class="upgrade-popup-text">To do this you must upgrade to</p>
                <p class="upgrade-popup-text">the PRO version!</p>
                <span class="popup-close">X</span>
            </div>
        </div>
    `);
    $(".popup-close").click(function() {
        console.log('closing')
        $(".upgrade-popup-container").remove();
    });
}

My problem is that the click function is not working on the .popup-close span for some reason. 我的问题是,由于某种原因, click功能无法在.popup-close span工作。 I know that it has to do with the instagram post modal being open because I use this custom pop-up in other places/pages when there is no post modal open and it works great. 我知道这与instagram帖子模式的打开有关,因为当没有帖子模式打开且效果很好时,我会在其他地方/页面使用此自定义弹出窗口。 But when the instagram post modal is open the .popup-close span does nothing when I click it. 但是当instagram post modal打开时,当我单击它时, .popup-close span .popup-close

Why is this happening? 为什么会这样呢?

I know that it has nothing to do with z-index because I have tested that. 我知道与z-index无关,因为我已经测试过了。 I feel like Instagram might be running some kind of jQuery in the background that is disrupting my click event from binding because even when I open both modals and then paste the click code straight into the console the .popup-close span still does nothing. 我觉得Instagram可能会在后台运行某种jQuery,这会破坏我的click事件的绑定,因为即使我同时打开两个模式,然后将click代码直接粘贴到控制台中, .popup-close span仍然无济于事。

UPDATE: I have also tried event delegation for the .popup-close span and that does not work. 更新:我还尝试了.popup-close span事件委托, .popup-close不起作用。

UPDATE: I have also tried to bind to the .popup-close span with vanilla javascript and that does not work. 更新:我也试图用香草javascript绑定到.popup-close span ,并且不起作用。 It seems like nothing can bind to this element when the instagram post modal is up. instagram帖子模式启动后,似乎没有任何东西可以绑定到此元素。

You can use $('body').on('click', eventHandlerFunction); 您可以使用$('body').on('click', eventHandlerFunction);

Documentation here . 文档在这里

function attachUpgradePopup(str) {
    $(".upgrade-popup-container").remove();
    $("body").append(`
        <div class="upgrade-popup-container">
            <div class="upgrade-popup">
                <img class="popup-img" src="https://i.imgur.com/6sOdwYs.png">
                <p class="upgrade-popup-text">This post is ${str}.</p><br>
                <p class="upgrade-popup-text">To do this you must upgrade to</p>
                <p class="upgrade-popup-text">the PRO version!</p>
                <span class="popup-close">X</span>
            </div>
        </div>
        `);
    $("body").on("click", ".popup-close", function () {
        console.log('closing')
        $(".upgrade-popup-container").remove();
    });
}

Basically $("body") will capture click and check if target element is matching ".popup-close". 基本上$("body")将捕获点击并检查目标元素是否匹配“ .popup-close”。

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

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