jQuery + window.location.hash 和同页锚点 - 不一致的行为
jQuery + window.location.hash and same-page anchors - inconsistent behavior?
考虑以下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>
它的作用是首先检查是否登陆具有 #anchor 的页面,或者用户是否单击了同一页面 #anchor,然后简单地使用相应 id 对目标div 进行动画处理。
问题是这 2 个交替工作:如果您登陆带有哈希符号的页面,该页面将动画化为div ID,但随后的同页链接不会被绑定的"click"事件拦截(我也尝试过将其与 live() 绑定,没有区别)
如果您使用干净的URL登陆页面,则链接将再次起作用。我做错了什么?
为什么返回 false?这没有任何意义,因为在"ready"事件处理程序中,没有可以阻止的默认行为或将冒泡的 DOM 树。此外,它还会阻止执行以下语句(特别是将事件处理程序绑定到链接)。
if (window.location.hash.length) {
$('html,body').animate({scrollTop: 200}, 100);
return false; // <-- remove this line!
}
相关文章:
- 当属性不一致时,如何根据属性对JS对象列表进行排序
- Android和JavaScript解析API之间不一致
- Javascript的行为与PHP包含文件不一致
- 对这种与document.domain和CORS相关的不一致行为的解释是什么
- 点击链接行为不一致
- Passport.js`isAuthenticated()`不一致的行为;当它应该是真的时候是假的
- 与显示不一致
- Facebook标签页更改url时不带window.location
- Ext 4.2.1 模型日期解析在浏览器之间不一致
- 通过 Dropbox API 上传时的文件内容不一致
- JavaScript - 对象属性不一致
- Javascript 混淆了 null、instanceof 和 typeof 的语法不一致
- localeCompare 显示使用前导变音字符对单词进行排序时不一致的行为
- SVG 对象部分上的 JQuery click() 不一致
- IPAD safari 浏览器不支持 window.open JavaScript 方法
- Window.open浏览器的高度不一致
- jQuery + window.location.hash 和同页锚点 - 不一致的行为
- window.history.pushState uri编码不一致
- Window.location.reload (true)工作不一致
- 浏览器语言检测:用户代理与window.navigator.language不一致