我的代码使用 jquery 向页面添加元素,然后向其添加“点击”事件。代码是:
$('.photosItem').mouseover(function() {
// Remove all existing comment buttons
$('.reelPhotoPageCommentLink').remove();
// Add a comment button
var commentButton = "<div class='reelPhotoPageCommentLink'>";
commentButton += "Comment";
commentButton += "</div>";
$(this).prepend(commentButton);
// Add click event to comment button
$('.reelPhotoPageCommentLink').click(function() {
$('#editPopupForm').remove();
// Get photo id
var photoID = $(this).parent().attr('photo_id');
var url = "get_photo_comment_form/" + photoID;
$.ajax({
url: url,
type: "POST",
success: function(data) {
// Add item after header
$('#header').after(data);
}
});
});
});
因此,当您将鼠标悬停在具有“photosItem”类的照片上时,会出现一个“评论”按钮。点击按钮弹出评论框。这在 Firefox 上运行良好,但我在 Chrome 上遇到了问题,它似乎没有接收到点击事件。当我悬停时它会添加评论按钮,但单击它不会做任何事情。控制台在任何阶段都没有错误。
我在 $('.reelPhotoPageCommentLink').click(function() { 之后添加了一个 console.log,它没有显示出来,所以看起来点击事件被忽略了。
任何人有任何想法我怎样才能让它工作?在 Firefox 中没问题,那里没有警告或错误。
谢谢!