简体   繁体   English

将JQuery ON与用于动态元素的插件一起使用

[英]Using JQuery ON with Plugins for Dynamic Elements

I am trying to use a JQuery modal plugin called Magnific Popup to load AJAX content in to a modal. 我正在尝试使用一个名为Magnific Popup的JQuery模态插件将AJAX内容加载到模态中。 That said, this question applies to really any plugin that can bind to an element(s). 就是说,这个问题实际上适用于任何可以绑定到元素的插件。

How do I bind plugins to dynamic elements? 如何将插件绑定到动态元素? I have tried the following basic code that I found in response to another question, without any luck. 我尝试了以下基本代码,这些代码是我在回答另一个问题时发现的,没有任何运气。 And I've tried it both inside document.ready and outside. 我已经在document.ready内部和外部都尝试过。

$(document).on('click', '.pix', function(){
    $(this).magnificPopup({
        type: 'ajax',
        alignTop: true,
        overflowY: 'scroll'
    });
});

Just using $('.pix').magnificPopup works on existing elements. 仅使用$('。pix')。magnificPopup即可在现有元素上使用。

EDIT: I see my issue in the code above, I am binding the event on the click... but I need to bind the modal event on element load. 编辑:我在上面的代码中看到我的问题,我在单击上绑定事件...但是我需要在元素加载上绑定模式事件。 But how? 但是如何?

try livequery plugin 试试livequery插件

https://plugins.jquery.com/livequery/ https://plugins.jquery.com/livequery/

usage 用法

$(".pix").livequery(function() {
    $(this).magnificPopup({
        type: 'ajax',
        alignTop: true,
        overflowY: 'scroll'
    });
});

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

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