为什么我的页面在每个帖子后都会上升

时间:2016-07-22 18:18:58

标签: javascript jquery html

    var main = function(){
  $('.btn').click(function() {
     var post = $('.status-box').val();
     $('<li>').text(post).prependTo('.posts');
     $('.status-box').val('');
     $('.counter').text('140');
     $('.btn').addClass('disabled');
  });
  $('.status-box').keyup(function(){
    var postLength = $(this).val().length;
    var charactersLeft = 140 - postLength;
    $('.counter').text(charactersLeft);
    if (charactersLeft < 0){
        $('.btn').addClass('disabled');
      }
      else if (charactersLeft == 140){
        $('.btn').addClass('disabled');
      }
      else {
        $('.btn').removeClass('disabled');
      }
  });
   $('.btn').addClass('disabled');
};

$(document).ready(main);

每次发帖时,我的页面都会一直走到顶端,为什么会发生这种情况?我有一个div,可以选择将140个字符的帖子放入列表并显示在页面上。每当我发布其中一个帖子时,网页就会显示在顶部并看到帖子,你必须一直向下滚动。

1 个答案:

答案 0 :(得分:5)

最有可能的是,除了自定义事件逻辑之外,还会执行默认表单提交或链接跟随行为。它可能会导航到#或页面顶部附近的其他锚点。为了防止这种情况发生变化

$('.btn').click(function() {
    ...

$('.btn').click(function(event) {
    event.preventDefault();
    ...
相关问题