javascript 我可以使用 jQuery $(document).on('each', '.showComments', function(e) {})

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19595039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:07:03  来源:igfitidea点击:

Can I use jQuery $(document).on('each', '.showComments', function(e) {})

javascriptjqueryhtml

提问by Brijesh

I am using jquery UI dialog to show comments or other text depending upon what is clicked.

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

Here is my JSfiddle link Dialog Demo

这是我的 JSfiddle 链接对话框演示

I have used the code

我已经使用了代码

$('.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
    }
});

and the content is added dynamically on page load. I am trying to use $(document).on('each', '.showComments', function(e) {}); so that it can work with dynamically loaded content, but it doesn't work at all. here is my modified code.

并且在页面加载时动态添加内容。我正在尝试使用 $(document).on('each', '.showComments', function(e) {}); 这样它就可以处理动态加载的内容,但它根本不起作用。这是我修改后的代码。

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

but this doesn't work at all. Am i doing something wrong.

但这根本行不通。难道我做错了什么。

Thanks for the help.

谢谢您的帮助。

采纳答案by nzifnab

If the .divContentDetailis added dynamically after page load, it's not the loop you need to change, but the event that you are registering:

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

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

回答by Karl-André Gagnon

.onbind event that work on dynamicly added elements. But 'each'is not an event, it's a method.

.on作用于动态添加元素的绑定事件。但'each'它不是一个事件,它是一种方法。

You should use on like that :

你应该像这样使用:

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


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