gpt4 book ai didi

javascript - 通过 jQuery 附加的 HTML 不会接受点击事件

转载 作者:行者123 更新时间:2023-11-30 12:49:48 27 4
gpt4 key购买 nike

最近几天这种行为已经阻止了我。我安装了 waypoint jQuery 插件,当到达页面底部时它会加载更多评论。加载的 HTML 包括一些带有类的按钮,这些按钮也必须通过 通过 jQuery 在点击时触发。这是行不通的。

所以这个场景很好地总结了它:

  • 加载文档的评论的喜欢、不喜欢和删除按钮正常工作
  • 用户到达页面底部,jQuery 通过 AJAX 调用将更多评论加载到页面
  • 来自 AJAX 评论的喜欢、不喜欢和删除不会触发事件。

为什么?我不明白。他们有正确的类(class)和一切。

HTML:

<div class="comments">
<div id="new-comment"></div>

<script>var commenters = [{{ S_IMAGE_COMMENTERS }}];</script>

{% for comment in S_IMAGE_COMMENTS %}
<div id="cid-{{ comment.comment_id }}" class="comments-area row{% if loop.last %} last-comment{% endif %}">
<div class="user-info col-lg-2 col-md-2 col-sm-2 right">
<img src="{{ comment.user_avatar }}" height="75px" alt="Avatar" class="comment-avatar valign-top img-circle cream-thick-border" />
</div>
<div class="col-lg-1 col-md-1 col-sm-1" style="width: 2%;"></div>
<div class="comment-rag col-lg-9 col-md-9 col-sm-9 left" style="padding: 0;">
<div class="comment-content row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 break">
<span class="small comment-stats">
Posted by <strong><a href="{{ S_WEB_PATH }}user/{{ comment.username }}">{{ comment.username }}</a></strong> about {{ comment.comment_time }}
<span class="grey">
&nbsp;/&nbsp;<span class="clikesCount">{{ comment.comment_likes }}</span> likes
&nbsp;&bull;&nbsp;<span class="cdislikesCount">{{ comment.comment_dislikes }}</span> dislikes
</span>
</span>
<span class="pipe-hide">&nbsp;|&nbsp;</span>
<span class="comment-options">
<button value="{{ S_IMAGE_NAME }}?do=comment{% if comment.comment_id == comment.liked_cid and comment.like_type != '' and comment.like_type == 'like' %}un{% endif %}like" class="likeUnlike"><span class="comment-option glyphicon glyphicon-thumbs-up"{% if comment.comment_id == comment.liked_cid and comment.like_type != '' and comment.like_type == 'like' %} style="color: #aa4e4e;"{% endif %} title="{% if comment.comment_id == comment.liked_cid and comment.like_type != '' and comment.like_type == 'like' %}Unlike{% else %}Like{% endif %}"></span></button>

<button value="{{ S_IMAGE_NAME }}?do={% if comment.comment_id == comment.liked_cid and comment.like_type != '' and comment.like_type == 'dislike' %}remove_{% endif %}commentdislike" class="dislikeUnlike"><span class="comment-option glyphicon glyphicon-thumbs-down"{% if comment.comment_id == comment.liked_cid and comment.like_type != '' and comment.like_type == 'dislike' %} style="color: #aa4e4e;"{% endif %} title="{% if comment.comment_id == comment.liked_cid and comment.like_type != '' and comment.like_type == 'dislike' %}Remove Dislike{% else %}Dislike{% endif %}"></span></button>
{% if comment.commenter_id != S_USER_ID %}<button><span class="comment-option glyphicon glyphicon-minus-sign" title="Report"></span></button>{% endif %}

{% if comment.commenter_id == S_USER_ID %}<button value="{{ S_IMAGE_NAME }}?do=delete_comment" class="deleteComment comment-option close" title="Delete">&times;</button>{% endif %}

</span>

<span class="comment-content-main break">{{ comment.comment_content }}</span>
</div>
</div>
</div>
</div>
<hr{% if loop.last %} style="display: none"{% endif %} class="line-{{ comment.comment_id }}" />
{% else %}
<div class="row noComments">
<br />
<div class="col-lg-12 center">
<h3><span>There are no comments for this image.</span></h3>
</div>
</div>
{% endfor %}
</div><!-- .comments -->

jQuery(负责加载更多评论):

    // Load more comments when bottom's reached
var $loading = $('<div class="loading row" style="display: none;"><div class="col-lg-12 center">' + generate_preloader(32, 3) + '</div></div>');
var $footer = $('.last-comment');
var $docURL = document.URL;
var $imageName = $docURL.substring(-5);
var $start = { from: 10 };
var opts = { offset: '100%' };

//alert($imageName);

$footer.waypoint(function(event, direction) {

var $commentCount = document.getElementsByClassName('comments-area');

if ($commentCount.length < 10)
{
return;
}

$footer.waypoint('destroy');
$('.loadMore').append($loading);
$('.loading').fadeIn('slow');

delay('comments', function(){
$.ajax({
type: 'GET',
url: $imageName + '?do=fetchMoreComments&start=' + $start.from,
success: function(data){

// console.log(data); return;
var response = $.parseJSON(data);

if ('error' in response)
{
$loading.fadeOut('fast');
return;
}

$start.from += 10;
$loading.detach();
$footer.waypoint(opts);

// Remove previous last class
$('.comments-area').removeClass('last-comment');

var commentsLength = response.comments.length - 1;

for (var key in response.comments)
{
var comment = response.comments[key];

var commentHTML = ((key == 0) ? '<hr />' : '') + '<div id="cid-' + comment.comment_id + '" class="comments-area row' + ((commentsLength == key) ? ' last-comment' : '') + '" style="display: none;">';
commentHTML += '<div class="user-info col-lg-2 col-md-2 col-sm-2 right">';
commentHTML += '<img src="' + comment.user_avatar + '" height="75px" alt="Avatar" class="comment-avatar valign-top img-circle cream-thick-border" />';
commentHTML += '</div>';
commentHTML += '<div class="col-lg-1 col-md-1 col-sm-1" style="width: 2%;"></div>';
commentHTML += '<div class="comment-rag col-lg-9 col-md-9 col-sm-9 left" style="padding: 0;">';
commentHTML += '<div class="comment-content row">';
commentHTML += '<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 break">';
commentHTML += '<span class="small comment-stats">';
commentHTML += 'Posted by <strong><a href="' + generate_site_url() + 'user/' + comment.username + '">' + comment.username + '</a></strong>';
commentHTML += '<span class="grey">';
commentHTML += '&nbsp;/&nbsp;<span class="clikesCount">' + comment.comment_likes + '</span> likes';
commentHTML += '&nbsp;&bull;&nbsp;<span class="cdislikesCount">' + comment.comment_dislikes + '</span> dislikes';
commentHTML += '&nbsp;/&nbsp;' + comment.comment_time;
commentHTML += '</span>';
commentHTML += '</span>';
commentHTML += '<span class="pipe-hide">&nbsp;|&nbsp;</span>';
commentHTML += '<span class="comment-options">';

commentHTML += '<button value="' + $imageName + '?do=comment' + ((comment.comment_id == comment.liked_cid && comment.like_type != '' && comment.like_type == 'like') ? 'un' : '') + 'like" class="likeUnlike"><span class="comment-option glyphicon glyphicon-thumbs-up"' + ((comment.comment_id == comment.liked_cid && comment.like_type != '' && comment.like_type == 'like') ? ' style="color: #aa4e4e;"' : '') + ' title data-original-title="' + ((comment.comment_id == comment.liked_cid && comment.like_type != '' && comment.like_type == 'like') ? 'Unlike' : 'Like') + '"></span></button>';

commentHTML += '<button value="' + $imageName + '?do=' + ((comment.comment_id == comment.liked_cid && comment.like_type != '' && comment.like_type == 'dislike') ? 'remove_' : '') + 'commentdislike" class="dislikeUnlike"><span class="comment-option glyphicon glyphicon-thumbs-down"' + ((comment.comment_id == comment.liked_cid && comment.like_type != '' && comment.like_type == 'dislike') ? ' style="color: #aa4e4e;"' : '') + ' title data-original-title="' + ((comment.comment_id == comment.liked_cid && comment.like_type != '' && comment.like_type == 'dislike') ? 'Remove Dislike' : 'Dislike') + '"></span></button>';

// commentHTML += '<span class="comment-option glyphicon glyphicon-thumbs-down"></span>';
commentHTML += (comment.username != $('.user-username').text()) ? '<button><span class="comment-option glyphicon glyphicon-minus-sign" title="Report"></span></button>' : '';
commentHTML += (comment.username == $('.user-username').text()) ? '<button class="comment-option close" title="Delete">&times;</button>' : '';
commentHTML += '</span>';
commentHTML += '<span class="comment-content-main break">' + comment.comment_content + '</span>';
commentHTML += '</div>';
commentHTML += '</div>';
commentHTML += '</div>';
commentHTML += '</div>';
commentHTML += '<hr' + ((commentsLength == key) ? ' style="display:none"' : '') + ' />';

$('.comments').append($(commentHTML));
$('.comments-area').slideDown('fast');
}
}
});
}, average_ajax_delay);

}, opts);

触发喜欢、不喜欢和删除按钮的jQuery:

// Comment like, dislike, unlike and remove dislike
$('.likeUnlike').click(function(e){

e.preventDefault();

var likeHref = $(this).val();
var commentID = $(this).parent().parent().parent().parent().parent().attr('id').substring(4);

var likeUnlike = (likeHref.indexOf('commentunlike') != -1) ? 'commentunlike' : 'commentlike';
var oppositePreg = (likeUnlike == 'commentlike') ? 'commentunlike' : 'commentlike';

var likeStr = (likeUnlike == 'commentlike') ? 'Unlike' : 'Like';

var likeUnlikeUrl = likeHref.replace(/comment(un)?like/g, oppositePreg);
var thumbColor = (likeUnlike == 'commentlike') ? '#aa4e4e' : '#A2A2A2';

$(this).val(likeUnlikeUrl);
$('#cid-' + commentID + ' .glyphicon-thumbs-up').attr('data-original-title', ucfirst(likeStr));
$('#cid-' + commentID + ' .comment-options .tooltip-inner').text(ucfirst(likeStr));
$('#cid-' + commentID + ' .comment-options .glyphicon-thumbs-up').css('color', thumbColor);

// Check if dislike is clicked
dislikeButton = $('#cid-' + commentID + ' .dislikeUnlike');
if (dislikeButton.val().indexOf('remove_commentdislike') != -1)
{
dislikeButton.val(dislikeButton.val().replace(/(remove_)?commentdislike/g, 'commentdislike'))
dislikeButton.children().attr('data-original-title', 'Dislike');
dislikeButton.children().css('color', '#A2A2A2');
}

$.ajax({
type: 'POST',
url: likeHref,
data: { commentID: commentID },
success: function(data){

// console.log(data); return;
var response = $.parseJSON(data);

if ('error' in response)
{
display_alert(response.error, 'danger', 3000, 'top');
// return;
}

$('#cid-' + commentID + ' .small.comment-stats .grey .clikesCount').replaceWith(response.clikeCount);
$('#cid-' + commentID + ' .small.comment-stats .grey .cdislikesCount').replaceWith(response.cdislikeCount);
}
});

return false;
});

$('.dislikeUnlike').click(function(e){

e.preventDefault();

var dislikeHref = $(this).val();
var commentID = $(this).parent().parent().parent().parent().parent().attr('id').substring(4);

var dislikeUnlike = (dislikeHref.indexOf('remove_commentdislike') != -1) ? 'remove_commentdislike' : 'commentdislike';
var oppositePreg = (dislikeUnlike == 'commentdislike') ? 'remove_commentdislike' : 'commentdislike';

var dislikeStr = (dislikeUnlike == 'commentdislike') ? 'Remove Dislike' : 'Dislike';

var dislikeUnlikeUrl = dislikeHref.replace(/(remove_)?commentdislike/g, oppositePreg);
var thumbColor = (dislikeUnlike == 'commentdislike') ? '#aa4e4e' : '#A2A2A2';

$(this).val(dislikeUnlikeUrl);
$('#cid-' + commentID + ' .glyphicon-thumbs-down').attr('data-original-title', ucfirst(dislikeStr));
$('#cid-' + commentID + ' .comment-options .tooltip-inner').text(ucfirst(dislikeStr));
$('#cid-' + commentID + ' .comment-options .glyphicon-thumbs-down').css('color', thumbColor);

// Check if dislike is clicked
likeButton = $('#cid-' + commentID + ' .likeUnlike');
if (likeButton.val().indexOf('unlike') != -1)
{
likeButton.val(likeButton.val().replace(/comment(un)?like/g, 'commentlike'))
likeButton.children().attr('data-original-title', 'Like');
likeButton.children().css('color', '#A2A2A2');
}

$.ajax({
type: 'POST',
url: dislikeHref,
data: { commentID: commentID },
success: function(data){

// console.log(data); return;
var response = $.parseJSON(data);

if ('error' in response)
{
display_alert(response.error, 'danger', 3000, 'top');
// return;
}

$('#cid-' + commentID + ' .small.comment-stats .grey .clikesCount').replaceWith(response.clikeCount);
$('#cid-' + commentID + ' .small.comment-stats .grey .cdislikesCount').replaceWith(response.cdislikeCount);
}
});

return false;
});

$('.deleteComment').click(function(e){

e.preventDefault();

var deleteHref = $(this).val();
var commentID = $(this).parent().parent().parent().parent().parent().attr('id').substring(4);

$.ajax({
type: 'POST',
url: deleteHref,
data: { commentID: commentID },
success: function(data){

// console.log(data); return;
var response = $.parseJSON(data);

if ('error' in response)
{
display_alert(response.error, 'danger', 3000, 'top');
return;
}

$('#cid-' + commentID).slideUp('fast');
$('.line-' + commentID).slideUp('fast');
}
});

return false;
});

因此,如前所述,随文档加载的 HTML(当页面首次加载时)具有正常工作的喜欢、不喜欢和删除按钮。但是,当新评论来自 AJAX 时,这些按钮不起作用。

如何解决这个问题?

最佳答案

您需要使用事件委托(delegate)来使用 on() 绑定(bind)事件用于将事件绑定(bind)到动态添加的元素,这些元素在执行 event 绑定(bind)代码时不存在。您可以将事件委托(delegate)给动态添加元素的 static 父级或文档。

$(document).on('click', '.deleteComment', function(e){

});

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers, Reference.

关于javascript - 通过 jQuery 附加的 HTML 不会接受点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21335737/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com