Javascript 链接到hashtag
我需要一些东西把这个链接起来Javascript 链接到hashtag,javascript,jquery,html,Javascript,Jquery,Html,我需要一些东西把这个链接起来 $(function() { $('a[href*=#]:not([href=#])').click(function() { if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { var target = $(this.has
$(function() {
$('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-55
}, 1000);
return false;
}
}
});
});
但它只适用于该链接
。我希望此代码适用于此链接,例如
我认为您的jquery选择器是错误的
看
请注意,片段标识符有时很难测试:如果您在地址栏中手动更改它,您将需要强制页面刷新,因为只需按enter键实际上不会有多大作用。
$(function() {
$("a[href*='#']:not([href='#'])").click(function(event) {
location = $(this).attr('href');
event.stopPropgation();
return true;
});
});