防止自动向下滚动Jquery?

时间:2014-10-09 16:00:49

标签: javascript jquery html css

我有以下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时,页面会自动向下滚动。我知道这是因为页面大小的变化而发生的,但我怎样才能防止自动滚动发生。

感谢。

1 个答案:

答案 0 :(得分:1)

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

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

或者在结尾处返回false:

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