简体   繁体   English

如何从url中删除同一页面的超链接名称?

[英]How to remove same page hyperlink name from url?

My webpage is long so in the heading I have linked buttons to different parts of my page. 我的网页很长,因此在标题中,我已将按钮链接到页面的不同部分。 But when I click them, it changes from, for example, www.helloworld.com to www.helloworld.com/#part1 . 但是,当我单击它们时,它会从例如www.helloworld.com更改为www.helloworld.com/#part1。 How do I remove this "part1"? 如何删除此“ part1”? I know how to remove .html .php , but i dont know how to remove this. 我知道如何删除.html .php,但是我不知道如何删除它。

<li><a href="#part1" class="current"><strong>Story</strong></a></li>

This is the button and it is linked to 这是按钮,它链接到

<div id="part1"></div>

Thanks. 谢谢。

OKay, if you wanna make it smooth-scroll and remove the link follow the page, you can do so only by using JavaScript. 好的,如果您想使其平滑滚动并删除页面后的链接,则只能使用JavaScript来进行。 Either add a return false to the onclick event of the anchor or provide an alternate mechanism: 向锚的onclick事件添加return false或提供备用机制:

<li><a href="#part1" class="current" onclick="return false;"><strong>Story</strong></a></li>

The above code doesn't add the #part1 to the URL, but it also doesn't let you navigate to the <div> . 上面的代码没有将#part1添加到URL,但也不允许您导航到<div> So, you need to do this: 因此,您需要这样做:

<li><a href="#part1" class="current" onclick="window.scrollTo(0, document.getElementById('part1').offsetTop); return false;"><strong>Story</strong></a></li>

The return false; return false; will make the link not add the extra part. 将使链接不添加多余的部分。 And the window.scrollTo navigates the page to the right position. 然后window.scrollTo将页面导航到正确的位置。

To make this scalable for all the internal links, for a relatively new browser, that supports document.queryStringAll , do this: 为了使它对于所有内部链接都可扩展,对于支持document.queryStringAll的相对较新的浏览器,请执行以下操作:

document.queryStringAll('a[href^="#"]').onclick = function (e) {
  e.preventDefault();
  window.scrollTo(0, document.getElementById(this.attribute("href").substr(1)).offsetTop);
  return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM