我有以下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;
}):

关于javascript - 防止自动向下滚动Jquery?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26282932/

10-12 12:24
查看更多