我可以使用jQuery $(document).on('each','.showComments',function(e){}) [英] Can I use jQuery $(document).on('each', '.showComments', function(e) {})
问题描述
I am using jquery UI dialog to show comments or other text depending upon what is clicked.
jsfiddle.net/brijesh26/6wMRe/3/rel =nofollow> Dialog Demo
Here is my JSfiddle link Dialog Demo
我已经使用了代码
$('.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','.showComments',function(e){});所以它可以与动态加载的内容一起工作,但它根本不起作用。这里是我修改后的代码。
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 () {
var panel = $(this).parent().siblings('.divCommentDetail');
$(this).click(function () {
panel.dialog('open');
});
});
但这根本不起作用。我是否做错了什么。
but this doesn't work at all. Am i doing something wrong.
感谢您的帮助。
推荐答案
如果在页面加载后动态添加 .divContentDetail
,则不是您需要更改的循环,而是您正在注册的事件:
If the .divContentDetail
is added dynamically after page load, it's not the loop you need to change, but the event that you are registering:
$(document).on('click', '.showComments', function () {
var panel = $(this).parent().siblings('.divCommentDetail');
panel.dialog('open');
});
这篇关于我可以使用jQuery $(document).on('each','.showComments',function(e){})的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!