pol*_*111 6 html anchor jquery
$(document).ready(function(){
$('body a').click(function(e){
e.preventDefault();
var goTo = $(this).attr('href').replace('#','');
$('html, body').animate({
scrollTop:$('a[name="'+goTo+'"]').offset().top
},1775);
window.location.hash = "#"+goTo;
});
Run Code Online (Sandbox Code Playgroud)
我在我的代码中有这个功能来实现我页面上的滚动效果,但我认为这会影响我的图像链接.当我点击图像时,它不会链接到任何地方.我相当肯定错误在这里,但需要一些帮助找到它.
谢谢.
确保#
在更改任何内容之前首先在href中有一个哈希,这样普通链接仍然可以正常工作.
一种方法是检查hash
元素的属性
if(this.hash){
e.preventDefault();
// rest of code shown
}
Run Code Online (Sandbox Code Playgroud)
也可以使用属性选择器仅过滤掉#
href中的链接
$('body a[href^=#]').click...
Run Code Online (Sandbox Code Playgroud)
最后一个假设所有哈希链接都是相对的,并且href以 #