【问题标题】:Jquery not executing when content added after the document has loaded加载文档后添加内容时,Jquery未执行
【发布时间】:2013-12-13 14:06:35
【问题描述】:

我使用以下脚本在博客上的帖子上点赞

$('.like-unlike').on('click',function() {
    if ($(this).html() == " Like ") {
        postID = this.id.replace('post_', '');


        $.ajax({
            url: 'auth/classes/comments.php',
            type: 'GET',
            data: 'token=<?php echo $token; ?>&post='+escape(postID)+'&like=yes',

            success: function(data) {
                console.log(data);}
        });
        $('.right#stats_'+postID).text(function (idx, text) {
            text = $.trim(text);
            var count = parseInt(text.match(/^(\d+)/), 10) || 0;
            return text.replace(/^\d+/, ++count);
        });
        $(this).html('&nbsp;Liked&nbsp;');
    }

我实现了一个分页脚本,该脚本在页面加载后向服务器发送请求,以便在文档下载后将内容添加到页面中。现在我的喜欢脚本不起作用..如果我运行这个脚本firebug 它运行,请帮助

【问题讨论】:

  • 你应该用谷歌搜索你的标题,有一百个相关的答案......

标签: javascript php jquery events bing


【解决方案1】:

试试:

$('body').on('click','.like-unlike',function() {
    ...
});

【讨论】:

    【解决方案2】:
    $(document).on('click','.like-unlike',function() {
    

    Read about event delegation

    【讨论】:

    • 谢谢...不知道...如何使用 $(document).ready(function(){$(".comment_box").hide();});....这种格式?以便它隐藏comment_box,即使在文档下载后添加它? ——
    • 如果您有新问题,请单独发布。
    【解决方案3】:

    将事件处理程序更改为委托样式 -

    $('body').on('click', '.like-unlike', function() {
    

    【讨论】:

    • 谢谢...不知道...如何使用 $(document).ready(function(){$(".comment_box").hide();});....这种格式?这样即使在文档下载后添加评论框,它也会隐藏评论框?
    • 不,只有页面加载时可见的评论框会被隐藏。除非您再次隐藏它们,否则添加的任何其他人都会显示 - jsfiddle.net/m5MnF
    • 有没有一种方法可以隐藏所有 .comment_box 类,即使在文档加载之后?
    • @user3038421:使用 CSS。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 2012-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    相关资源
    最近更新 更多