阻止自动向下滚动Jquery

Prevent Auto scrolling down Jquery?

本文关键字:滚动 Jquery      更新时间:2023-09-26

我有以下JQuery代码

var o = true;
$('a#filter').click(function () {
    if(o){
        $('.heading').append(''
        <div id="fil">'
            <div class="row">'
                <div class="span12">'
                    <div class="content" style="padding-bottom:0;margin-top:0px; text-align:center; min-height:0px;background:-moz-linear-gradient(center top , #fafafa 0%, #e9e9e9 100%) repeat scroll 0 0 rgba(0, 0, 0, 0);background:-webkit-linear-gradient(top, #fafafa 0%, #e9e9e9 100%);">'
                        <?php echo '<a href="http://njace-cc.montclair.edu/admin/users/manage.php">All</a> ' . $letters; ?>'
                    </div>'
                </div>'
            </div>'
        </div>');
        o = false;
        $('#cls').html("▲");
    }
    else{
        $('.heading').children("#fil").remove();
        o = true;
        $('#cls').html("▼");
    }
});

基本上,它是关于添加和删除div的。然而,当我点击链接添加div时,页面会自动向下滚动。我知道这是因为页面大小的变化,但我该如何防止自动向下滚动。

谢谢。

滚动是锚链接的默认行为,为了停止它,您必须:

$('a#filter').click(function (event) {
  event.preventDefault();
  // your code
}):

或者最后返回false:

$('a#filter').click(function () {
  // your code
  return false;
}):