Javascript 我可以使用jQuery$(document).on(';each';,';.showComments';,函数(e){})

Javascript 我可以使用jQuery$(document).on(';each';,';.showComments';,函数(e){}),javascript,jquery,html,Javascript,Jquery,Html,我正在使用jQueryUI对话框根据单击的内容显示注释或其他文本 这是我的JSFIDLE链接 我已经使用了代码 $('.showComments').each(function () { var panel = $(this).parent().siblings('.divCommentDetail'); $(this).click(function () { panel.dialog('open'); }); }); $('.showContractC

我正在使用jQueryUI对话框根据单击的内容显示注释或其他文本

这是我的JSFIDLE链接

我已经使用了代码

$('.showComments').each(function () {
    var panel = $(this).parent().siblings('.divCommentDetail');
    $(this).click(function () {
        panel.dialog('open');
    });
});

$('.showContractChanges').each(function () {
    var panel = $(this).parent().siblings('.divContractChangeDetail');
    $(this).click(function () {
        panel.dialog('open');
    });
});


$(".divCommentDetail, .divContractChangeDetail").dialog({
    autoOpen: false,
    modal: true,
    open: function () {
        $(this).parent().siblings('.ui-dialog-titlebar').addClass('ui-state-error');
    },
    show: {
        effect: 'blind',
        duration: 1000
    },
    hide: {
        effect: 'explode',
        duration: 1000
    }
});
并且在页面加载时动态添加内容。我试图使用$(document).on('each','showcoments',函数(e){});因此,它可以处理动态加载的内容,但根本不起作用。这是我修改过的代码

$(document).on('each', '.showComments', function () {
    var panel = $(this).parent().siblings('.divCommentDetail');
    $(this).click(function () {
        panel.dialog('open');
    });
});
但这根本不起作用。我做错什么了吗


感谢您的帮助。

.on
绑定事件,该事件可处理动态添加的元素。但是
'each'
不是一个事件,而是一种方法

您应该这样使用:

$(document).on('click', '.showComments', function () {
    var panel = $(this).parent().siblings('.divCommentDetail');


    panel.dialog('open');
});

如果在页面加载后动态添加
.divContentDetail
,则需要更改的不是循环,而是要注册的事件:

$(document).on('click', '.showComments', function () {
  var panel = $(this).parent().siblings('.divCommentDetail');
  panel.dialog('open');
});

“each”不是被触发的事件,它只是一个循环。在更改之前,您的代码中是否存在不可用的内容?您好nzifnab,当第一次加载页面时,一切正常,但我的页面可以添加更多内容,并使用ajax刷新,一旦添加新内容,上述代码将停止工作。我在我的页面上看到了对话框div标签的内容,但没有看到对话框。嗨,Karl,谢谢你的回复,我尝试了你建议的代码,但它不起作用。