判断一个元素是否存在

发布于 2024-12-01 05:44:11 字数 2162 浏览 0 评论 0原文

我正在为Web应用程序创建一个评论系统,当用户单击评论图像时加载评论,使用json动态生成html代码来显示评论。 但是,当再次单击图像时,会再次发出请求,用相同的数据填充页面,

这是我的代码

function latest_pheeds() {
    var action = url+"pheeds/latest_pheeds";
    $.getJSON(action,function(data) {
        $.each(data,function(index,item) {
            $('#pheed-stream').append
            (
            '<div class="pheed" id="'+item.pheed_id+'">'+
            '<p><a href="">'+item.user_id+'</a></p>'+
            '<p>'+item.pheed+'</p>'+
            '<div class="pheed_meta">'+
            '<span>'+item.datetime+' Ago</span>'+
            '<span>'+item.comments+
            '<img class="comment_trigger" src="/pheedbak/assets/img/comment.png" title="Click to comment on pheed" onclick="retrieve_comments('+item.pheed_id+')">'+
            '</span>'+
            '</div>'+
            '</div>'
            );
        });
    });
}



function retrieve_comments(pheed_id) {
    var action = url+'comments/get_comments';
    var crsf =  $('input[name=ci_csrf_token]').val();
    var dataString = "pheed_id="+pheed_id+"&ci_csrf_token="+crsf;
    $.ajax({
        url:action,
        type:'POST',
        cache:false,
        dataType:'json',
        data:dataString,
        success:function(data) {
            $.each(data,function(index,item) {
                $("#" + pheed_id).append(
                '<div class="pheed_comments">'+
                    '<div class="comment">'
                    +'<span><p>'+item.user+'</p></span>'
                     +item.comment+
                    '</div>'+
                '</div>'+
                '<div id="comment_box">'+
                    '<textarea id="comment" cols="30">'+
                    '</textarea><br>'+
                    '<input type="button" class="submit_btn" value="comment" />'+
                '</div>'
                );
            });
        }
    });
}

latest_pheeds()加载pheeds,并retrieve_comments获取传递给它的pheed_id的评论。 我如何确定评论区域是否已填充,并使用新评论(如果有)更新它。谢谢

Am creating a comment system for a web app, the comments are loaded when a user clicks on the comment image, html code is dynamically generated with json to display the comments.
But when the image is clicked again,the request is made again populate the page with the same data

Heres my code

function latest_pheeds() {
    var action = url+"pheeds/latest_pheeds";
    $.getJSON(action,function(data) {
        $.each(data,function(index,item) {
            $('#pheed-stream').append
            (
            '<div class="pheed" id="'+item.pheed_id+'">'+
            '<p><a href="">'+item.user_id+'</a></p>'+
            '<p>'+item.pheed+'</p>'+
            '<div class="pheed_meta">'+
            '<span>'+item.datetime+' Ago</span>'+
            '<span>'+item.comments+
            '<img class="comment_trigger" src="/pheedbak/assets/img/comment.png" title="Click to comment on pheed" onclick="retrieve_comments('+item.pheed_id+')">'+
            '</span>'+
            '</div>'+
            '</div>'
            );
        });
    });
}



function retrieve_comments(pheed_id) {
    var action = url+'comments/get_comments';
    var crsf =  $('input[name=ci_csrf_token]').val();
    var dataString = "pheed_id="+pheed_id+"&ci_csrf_token="+crsf;
    $.ajax({
        url:action,
        type:'POST',
        cache:false,
        dataType:'json',
        data:dataString,
        success:function(data) {
            $.each(data,function(index,item) {
                $("#" + pheed_id).append(
                '<div class="pheed_comments">'+
                    '<div class="comment">'
                    +'<span><p>'+item.user+'</p></span>'
                     +item.comment+
                    '</div>'+
                '</div>'+
                '<div id="comment_box">'+
                    '<textarea id="comment" cols="30">'+
                    '</textarea><br>'+
                    '<input type="button" class="submit_btn" value="comment" />'+
                '</div>'
                );
            });
        }
    });
}

latest_pheeds() loads the pheeds and retrieve_comments gets the comments of the pheed_id passed to it.
How do i determine if the comment area has already been populated and instead update it with new comments if available. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠檬色的秋千 2024-12-08 05:44:11

检查

$("#" + pheed_id +' .pheed_comments').length

如果元素(我猜你的意思是 div.pheed_comments )不存在,是否为 0。

Check for

$("#" + pheed_id +' .pheed_comments').length

if will be 0 if the element(I guess vou mean div.pheed_comments ) doesn't exist.

雨后彩虹 2024-12-08 05:44:11

有几种选择:

1) 如果您已经拥有 ajax 调用中的所有注释,那么只需从 DOM 中删除之前的注释并插入您刚刚获得的所有内容即可。

2)如果你有一个ajax调用只能检索新评论,那么你可以检查DOM以查看DOM中是否已经有评论,然后在某个标记之后及时请求新评论(你之前会这样做)已保存)。当您收到新评论时,您会将它们附加到您已有的评论中。

$('#pheed-stream').children().length 将告诉您 pheed-stream 层次结构中有多少个子级。如果为零,则说明您还没有进行任何操作。

我建议,最简单的方法是检索所有评论并用新评论列表替换所有内容,而不是尝试仅检索新评论。仅检索新评论将需要某种时间令牌,您可以向服务器提供该时间令牌,以便服务器知道要给您哪些“新评论”。

$("#" + pheed_id + " .pheed_comments").length 将告诉您是否已检索到该 pheed_id 的任何评论。

Several choices:

1) If you already have all the comments from your ajax call, then just remove the previous comments you have from the DOM and insert everything you just got.

2) If you have an ajax call that can retrieve just the new comments, then you can just check the DOM to see if you already have comments in the DOM and then just request new comments after some token in time (that you would have previously saved). When you receive the new comments, you would append them to what you have.

$('#pheed-stream').children().length will tell you how many children there are in the pheed-stream hierarchy. If that's zero, you haven't done any pheeds yet.

I would suggest that it's probably easiest to just retrieve all the comments and replace everything you have with the new list of comments than to try to retrieve just the new comments. Retrieving just the new comments will require some sort of time token that you can give the server so it knows which "new comments" to give you.

$("#" + pheed_id + " .pheed_comments").length will tell you whether any comments have been retrieved yet for that pheed_id.

╄→承喏 2024-12-08 05:44:11

您正在添加带有 id 的 div 。只需检查它是否存在。如果是,则更新,否则插入。

you are adding div with id. Just check if it exists. If yes, then update it, otherwise insert.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文