Javascript - 滚动效果会干扰链接

时间:2015-08-17 14:09:23

标签: javascript scroll hyperlink anchor

   <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-footable/0.1.0/js/footable.min.js"></script>
<script type="text/javascript">
        $(function () {
            $('[id*=GridView1]').footable();
        });
    </script> 

有我的javascript代码,工作正常,除非我尝试点击链接时出现此错误:

TypeError:$(...)。offset(...)未定义

2 个答案:

答案 0 :(得分:0)

这是因为e.preventDefault(); 它会阻止链接起作用,并将其设置为正文中的所有标记

尝试使用其他脚本

答案 1 :(得分:0)

尝试:

   $('a[href*=#]:not([href=#])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
        || location.hostname == this.hostname) {

        var target = $(this.hash);
        target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
           if (target.length) {
             $('html,body').animate({
                 scrollTop: target.offset().top
            }, 1100);
            return false;
        }
    } 
});