简体   繁体   English

如何使用jQuery捕获已加载页面上的事件? (on()不起作用)

[英]How can I catch event on loaded pages with jQuery? ( on() not working)

I've a page in which I use the method Load() of jQuery to load some PHP code. 我有一个页面,其中使用jQuery的Load()方法Load()一些PHP代码。

In my base page, I have the following jQuery code: 在我的基本页面中,我有以下jQuery代码:

    $('.writeLink').on('click',function (e) {
    //alert("ok");
    var dataurl1 = $(this).attr("par1");
    var dataurl2 = $(this).attr("par2");
    if (dataurl1 != null)
    {
        $('#txtTo').val(dataurl1);
        $('#imgTo').attr('src',dataurl2);
        $('#writeModel').modal({onOpen: function (dialog) {
dialog.overlay.fadeIn('slow', function () {
    dialog.container.slideDown('slow', function () {
        dialog.data.fadeIn('slow');
    });
});

I have the #writemodel div on my base page. 我的基本页面上有#writemodel div。

The loaded PHP creates some divs with the .writeLink class, that should initiate the click event, but don't. 加载的PHP使用.writeLink类创建了一些div,这些div 应该会启动click事件,但不会。 I tried to use the .on() jQuery event but it doesn't work. 我试图使用.on() jQuery事件,但是它不起作用。

Any suggestions? 有什么建议么?

Try this code: 试试这个代码:

$("#writemodel").on("click", ".writeLink", function(e){
    //do stuff
});

The click event is captured by the parent container, but the function executes only if the event originated in one of the '.writeLink' divs. click事件由父容器捕获,但是仅当该事件起源于'.writeLink'div之一时,该函数才会执行。

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

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