Javascript 在添加jquery滚动功能后,无法导航到我网站中的其他页面
我正在为一家小公司创建一个网站。在网站的一个特定页面上,我这样做,当你点击目录中的一个标题时,它会向下滚动到页面的那个部分 这是我的HTML:Javascript 在添加jquery滚动功能后,无法导航到我网站中的其他页面,javascript,jquery,html,Javascript,Jquery,Html,我正在为一家小公司创建一个网站。在网站的一个特定页面上,我这样做,当你点击目录中的一个标题时,它会向下滚动到页面的那个部分 这是我的HTML: <!-- The title of the Problem Section in the Table of Contents--> <a href="#problem">Problem</a> <!-- Section of Page Titled Problem--> &l
<!-- The title of the Problem Section in the Table of Contents-->
<a href="#problem">Problem</a>
<!-- Section of Page Titled Problem-->
<h2 id="problem">Problem</h2>
当我点击目录中的问题时,url变成demo.com/home.html#problem
这个很好用。当我单击目录中的标题时,它会向下滚动到页面的该部分
我遇到的问题是,当我试图通过导航栏导航到网站中的另一个页面(如contact.html等)时,我无法
如何解决这个问题?因为您在JS中使用了
e.preventDefault()
,它阻止了锚的默认行为,即重定向。因此,为问题锚定提供一个类/id,并在js中使用它,这将是一个更好的选项对于链接操作,您需要删除e.preventDefault()
调用:
$("nav").find("a").click(function() {
var section = $(this).attr("href");
$("html, body").animate({
scrollTop: $(section).offset().top
});
})
删除
e.preventDefault()
。这就是阻止点击页面传输的原因,这很有效!非常感谢。很乐意帮忙。我加上它作为你的答案
$("nav").find("a").click(function() {
var section = $(this).attr("href");
$("html, body").animate({
scrollTop: $(section).offset().top
});
})