【发布时间】:2022-06-30 05:34:29
【问题描述】:
情况:我想从 URL 的末尾删除锚标签(#tag)
我尝试过的:我一直在关注“https://www.finsweet.com/hacks/15/”和“https://stackoverflow.com/questions/34175285/removing-anchor-tags-from-网址”。不过效果不是很好。
代码:
顶部导航栏中的我的 sn-p
<ul class="nav">
<li class="scroll-to-section">
<a href="#top" class="active">Home</a>
</li>
</ul>
我对 Id 的使用
<div class="main-banner header-text" id="top">
也许我对导航栏进行编辑的方式是错误的。但我不确定我需要做什么才能实现目标。或者我使用类和 ID 的方式可能不正确?
--- 编辑 1 ---
这是我试图用来从浏览器的 URL 栏中删除锚标记的脚本的剪辑器。
$("#js-anchor").click(function (evt) {
evt.preventDefault();
var anchor = $(this).text();
$("html, body").animate(
{
scrollTop: $("#" + anchor).offset().top,
},
1500
);
});
还有我正在查看的 html
<li class="scroll-to-section">
<a id="js-anchor" href="#testimonials" class="active"
>staff</a>
</li>
关于它的实验在这里:
https://the-md.studio/indexhash.html
EDIT2 我的新尝试
<li class="scroll-to-section">
<a href="#top" class="scroll-to active">Home</a>
</li>
JS
$(document).ready(function () {
// get the anchor link buttons
const menuBtn = $(".scroll-to");
// when each button is clicked
menuBtn.click(() => {
// set a short timeout before taking action
// so as to allow hash to be set
setTimeout(() => {
// call removeHash function after set timeout
removeHash();
}, 5); // 5 millisecond timeout in this case
});
// removeHash function
// uses HTML5 history API to manipulate the location bar
function removeHash() {
history.replaceState(
"",
document.title,
window.location.origin + window.location.pathname + window.location.search
);
}
});
【问题讨论】:
-
你想在哪里删除它?就像您在浏览器中编写代码一样?你能展示你的代码吗?
-
@TheFool 我在这里有一个开发网站空间:the-md.studio
-
您在尝试删除 href 的地方发布了零代码。如果您删除 href 它不再是有效链接(在您的示例中)那么您想要实现什么,以及如何实现?
-
@cloned 对此表示歉意。我基本上遵循了我提供的这两个链接,但没有任何结果。基本上在浏览器的 URL 栏中,它会显示锚标记,例如“index.html#tag”。我的目标是在导航时删除 URL 栏的锚部分。我将创建一个单独的页面以在可能的情况下显示它。谢谢
-
不要在互联网上的某处创建单独的页面,将此示例添加到您的问题中。您可以创建一个片段来显示您的行为。