Javascript jQuery+;window.location.hash和相同的页面定位-行为不一致?

Javascript jQuery+;window.location.hash和相同的页面定位-行为不一致?,javascript,jquery,html,anchor,Javascript,Jquery,Html,Anchor,考虑以下JS代码: <script type="text/javascript"> $(function() { // Check if landing on the page with a hash if (window.location.hash.length) { $('html,body').animate({scrollTop: 200}, 100); return false; } // Same-pag

考虑以下JS代码:

<script type="text/javascript">

$(function() {
    // Check if landing on the page with a hash
    if (window.location.hash.length) {
        $('html,body').animate({scrollTop: 200}, 100);
        return false;
    }

    // Same-page anchors
    $('a[href*=#]').click(function() {
        // ... find the target based on the div and animate the scrollTop property again
       }
    });
}); 
</script>

$(函数(){
//检查是否使用哈希值登录页面
if(window.location.hash.length){
$('html,body').animate({scrollTop:200},100);
返回false;
}
//同页锚
$('a[href*=#]')。单击(函数(){
//…根据div找到目标并再次设置scrollTop属性的动画
}
});
}); 
它所做的是首先检查是否使用“锚定”登录到页面,或者用户是否单击同一个页面“锚定”,并简单地使用相应的id设置到目标div的动画

问题是,这两种方法是交替工作的:如果你在一个带有哈希符号的页面上着陆,该页面将被设置为div ID的动画,但是随后相同的页面链接不会被绑定的“click”事件截获(我也尝试过用live()绑定它,没有区别)


如果您使用干净的URL登录页面,链接将再次工作。我做错了什么?

为什么返回false?这没有任何意义,因为在“就绪”事件处理程序中,没有可以阻止的默认行为,也没有将冒泡的DOM树。此外,它还防止执行以下语句(特别是事件处理程序与链接的绑定)

if(window.location.hash.length){
$('html,body').animate({scrollTop:200},100);
返回false//
if (window.location.hash.length) {
    $('html,body').animate({scrollTop: 200}, 100);
    return false;  // <-- remove this line!
}