HTML链接在您链接到的页面上时什么也不做 [英] HTML Links that do nothing when you are on the page they link to

查看:92
本文介绍了HTML链接在您链接到的页面上时什么也不做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在建立一个网站,其中包含很多指向我网站不同部分的链接(也称为导航)。允许我们调用这些链接及其相应页面 link1,page1 link2,page2 link3,page3 等。

I am currently building a website containing lots of links to different sections of my website (also known as navigation). Lets call those links and their corresponding pages link1, page1 , link2, page2, link3, page3 etc.

它们的一般代码是这样的:

The general code for them is this:

<a href="/page1.html">Link1</a>

<a href="/page2.html">Link2</a>

<a href="/page3.html">Link3</a>

我希望用户点击每个链接来移动到相应的网页,它的工作原理与此相同。问题是,当用户与他们点击的链接在同一页面上时,我希望链接什么也不做(意味着它只会重新加载页面)。让我通过一个例子来说明这一点:

I want the user to click each link to move to the corresponding webpage and it works as supposed to. The problem is that I want the links to do nothing when the user is on the same page as the link they clicked (meaning it will only reload the page). Let me make this clear by using an example:

当前使用情况:用户在 page1 上。用户点击 link1 。浏览器将重新加载 page1

期望的用途:用户位于 page1 。用户点击 link1 。浏览器什么都不会做。

Current use: User is on page1. User clicks on link1. The browser will reload page1.
Desired use: User is on page1. User clicks on link1. The browser will do nothing.

TL; DR基本上我在寻找一个if子句。我读过HTML中没有if子句,仅仅是因为它是一种标记语言,但是实现它的另一种方式是什么?感谢您的帮助。

TL;DR Essentially I am searching for an if clause. I have read there is no if clause in HTML simply because it's a markup language but what is another way to implement this? Thanks for your help.

推荐答案

添加一个/#!.这将防止滚动到顶部,也会阻止页面重新加载。
代码:

Add a /#!. This will prevent the scrolling to the top and will also prevent the page reloading. Code:

<a href="/page1.html/#!">Link1</a>

原始答案:

Original answer:


How do I stop a web page from scrolling to the top when a link is clicked that triggers JavaScript?

这篇关于HTML链接在您链接到的页面上时什么也不做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆