livequery 在 DOM 插入后不绑定
我不明白为什么 livequery
不绑定事件,但我必须使用 .click
。 这只是一个示例,它也可能使用 .click()
,但在实际代码中我被迫使用 livequery
。 有谁知道为什么 livequery 不起作用?
function bind_remove(comment){
var id = comment.attr('comment_id');
comment.find(".remove").livequery("click", function(e){
$.post("/deleteComment", {id: id}, function(response){
comment.remove();
comments = comments_container.find('.comment');
});
});
}
$(document).ready(function(){
var comments_container = $('#comments_container');
var comments = comments_container.find('.comment');
comments.each(function(){
bind_remove($(this));
});
$(".submit_button").livequery("click", function(e){
$.post("/newComment", {text: textarea.val()}, function(response){
comments_container.last().append($(response).fadeIn('slow',function(){
comments = comments_container.find('.comment');
bind_remove(comments.last());
}));
});
});
});
I don't understand why livequery
doesn't bind the event, but I have to use .click
.
This is just an example, which might also use the .click()
, but in the real code I'm forced to use livequery
.
Does anyone know why livequery
isn't working?
function bind_remove(comment){
var id = comment.attr('comment_id');
comment.find(".remove").livequery("click", function(e){
$.post("/deleteComment", {id: id}, function(response){
comment.remove();
comments = comments_container.find('.comment');
});
});
}
$(document).ready(function(){
var comments_container = $('#comments_container');
var comments = comments_container.find('.comment');
comments.each(function(){
bind_remove($(this));
});
$(".submit_button").livequery("click", function(e){
$.post("/newComment", {text: textarea.val()}, function(response){
comments_container.last().append($(response).fadeIn('slow',function(){
comments = comments_container.find('.comment');
bind_remove(comments.last());
}));
});
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
用这个替换
Try replacing
with this
我在最后一条评论中添加了一个随机 ID,然后使用 $('#myid') 选择它,而不是使用 'last()'。然后我绑定它并开始工作
I added a random id to the last comment, then I selected it with $('#myid'), not using 'last()'. Then I bind it and started to work