简体   繁体   English

jQuery on()在加载的元素上不起作用

[英]jQuery on() not working on loaded elements

Im doing everything like said in documents but it just isn't firing. 我正在做文档中所说的所有事情,但并没有解雇。

    //Magnific Popup Background Blur

    $('a.video-popup').on('click', function(e) {

        $('.main-wrapper').addClass('blur');
        $('header').addClass('blur');

        console.log('Easy!');

    });

    $('document.body').on('click','.mfp-wrap', function(e) {  //mfp-wrap is background

        console.log('Finallly!!');

        $('.main-wrapper').removeClass('blur');
        $('header').removeClass('blur');
    });

First function works very well but the second one.. 第一个功能效果很好,但第二个功能..

Situation: 情况:

  • If a.video-popup is pressed, popup appears with background 如果按a.video-popup,则弹出窗口带有背景
  • Popup elements are not in DOM when page is loaded 加载页面时,弹出元素不在DOM中
  • Popup elements are directly under body if loaded 弹出元素在加载后直接位于主体下面
  • If background is clicked, popup closes 如果单击背景,则弹出窗口关闭
  • I get nothing - doesn't remove classes or give consol.log, also no errors 我什么也没得到-不删除类或提供consol.log,也没有错误

SOLUTION: 解:

$('document.body').on('click','.mfp-wrap', function(e) 

to

$(document).on('click','.mfp-wrap', function(e)

in this selector 在这个选择器中

$('document.body')

means document node which contain body class not body node 表示包含body类而不是body节点的document节点

and there is no node which name is document 没有名称为document节点

Try like this 这样尝试

$(document).on('click','.mfp-wrap', function(e)

Try substituting 尝试替代

$(document.body).on('click','.mfp-wrap'

for 对于

$('document.body').on('click','.mfp-wrap'

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

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