Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 发布后滚动至评论_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 发布后滚动至评论

Javascript 发布后滚动至评论,javascript,jquery,ajax,Javascript,Jquery,Ajax,我想让用户自动滚动到用户刚刚对一篇文章发表的评论。用户通过Ajax调用提交其评论。通过Javascript,我检查用户是否获得成功消息。如果用户填写了,则表示他/她已正确填写了所有内容。然后是时候刷新页面并跳转到他/她的评论 URL看起来像:domain.com/article/comment-73 现在我的问题是,如何通过Javascript跳转到他/她的评论?这是我目前的JS $(function() { $(document).on('click',"#send-comment-

我想让用户自动滚动到用户刚刚对一篇文章发表的评论。用户通过Ajax调用提交其评论。通过Javascript,我检查用户是否获得成功消息。如果用户填写了,则表示他/她已正确填写了所有内容。然后是时候刷新页面并跳转到他/她的评论

URL看起来像:domain.com/article/comment-73

现在我的问题是,如何通过Javascript跳转到他/她的评论?这是我目前的JS

$(function() { 
    $(document).on('click',"#send-comment-btn",function(e) {
    e.preventDefault();

        $.ajax({
            type: "POST",
            url: "/includes/db-requests/db-post-comment-on-article.php",
            data: $("#comment-recaptcha-form").serialize(),
            success: function(data,textStatus,jqXHR){   comment_on_article(data,textStatus,jqXHR);  }
        });
    });
});

function comment_on_article(data,textStatus,jqXHR) {
    alert(data);

    if (data == "comment_wrong_captcha") {
        //throw error message
    }

    if (data == "comment_finished_success") {   
        $('#comment-captcha-handler').removeClass('error-msg');
        $('#comment-captcha-handler').text('Your comment has been posted!');
        $('#comment-captcha-handler').addClass('success-msg');
        $('#comment-captcha-handler').slideDown(400);

        setTimeout(function (){
            location.reload(); //Jump to comment after reload????
            $("#comment-article-dialog").dialog( "close" );
        }, 2000);

    }
}

任何帮助都将不胜感激

因为您说要刷新页面,您可以使用纯html,通过让服务器端代码为每篇文章添加一个锚,如:

<a id="post_POST_ID"></a>
并将post\u post\u ID附加到刷新url 这将使浏览器向下跳转,使定位点位于左上角的行可以使用:


如果必须重新加载,用ajax提交评论有什么意义-/于是出现了新的评论。但我想我可以将它们加载到一个div中,并在提交新评论时刷新div;这就是我一直在尝试的,但我无法让JS识别注释id…:|你不需要javascript,这是一个浏览器/html的东西。实际上,您正在使用类似window.location=您的url这样的调用刷新页面,对吗?顺便说一句,您为什么不在客户端验证输入并使用submit按钮进行常规发布呢。您还需要验证输入服务器端,但是如果用户篡改您的javascript只是为了发布恶意数据,那么我认为您不必担心他或她没有发布他的评论:PHow您是否构建了返回数据?当你使用alertdata时,你会得到什么?我会将其格式化为:SUCCESSORFAILURMESSAGE;POSTID并执行var dataSplit=data.split';';阅读dataSplit[0]中的消息和dataSplit[0]中的id我也会使用JSON,如果您有时间/兴趣,您应该阅读它。然而,如果这是一个小型的星期五项目,并且您不必担心字符串拆分的效果会和阅读:学习JSON一样好,那么它是值得的
// Plain javascript 1:
window.scrollTo(x, y);

// Plain javascript 2:
window.location = '#comment-72'; // #=hash, not like jQuery's ID-selector

// jQuery (animated):
$("html, body").animate({ scrollTop: x }, 1000);

// jQuery (animated) to certain element:
$("html, body").animate({ scrollTop: $('#yourElement').offset().top }, 1000);