href=“#”在页面 url 末尾附加 #

发布于 2024-10-31 05:28:53 字数 318 浏览 0 评论 0原文

假设我有以下链接:

<a href="#" onclick="alert('You clicked a link.');">Click Me!</a>

单击此链接时将发出一条警报消息,并在页面 URL 末尾附加井号。
这看起来不太漂亮,除了在 url 本身中使用 javascript 之外,还有什么方法可以避免它:

<a href="javascript:alert('You clicked a link.');">Click Me!</a>

Let's say I have the following link:

<a href="#" onclick="alert('You clicked a link.');">Click Me!</a>

When clicked this link will alert a message as well as appending a pound sign on the end of the page url.
This doesn't look very pretty is there any way to avoid it besides using javascript in the url itself:

<a href="javascript:alert('You clicked a link.');">Click Me!</a>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪漫之都 2024-11-07 05:28:53

您必须防止发生默认响应。

老式的方法是从中返回 false

<a href="#" onclick="alert('You clicked a link.'); return false;">Click Me!</a>

或者,更好:

<a href="#" id="myLink">Click Me!</a>

<script type="text/javascript">
window.onload = function() {
   document.getElementById('myLink').onclick = function(event) {
      alert('You clicked a link.');
      return false;
   };
};
</script>

现在最好的方法是调用 event 属性的正确方法:

<a href="#" id="myLink">Click Me!</a>

<script type="text/javascript">
window.onload = function() {
   document.getElementById('myLink').onclick = function(event) {
      alert('You clicked a link.');
      event.preventDefault(); // <---
   };
};
</script>

最好替换对于不使用 JavaScript 的人来说,# 带有某个正确页面的 URI。在某些司法管辖区,无障碍实际上是一项法律要求。

编辑针对 IE 进行了修复:

function f() {
   document.getElementById('myLink').onclick = function(e) {
      alert('You clicked a link.');

    if (!e) {
       var e = window.event;
    }

    // e.cancelBubble is supported by IE - this will kill the bubbling process.
    e.cancelBubble = true;
    e.returnValue  = false;

    // e.stopPropagation works only in Firefox.
    if (e.stopPropagation) {
        e.stopPropagation();
        e.preventDefault();
    }
};

window.onload = f;

You have to prevent the default response from occurring.

The old-fashioned approach is to return false from it:

<a href="#" onclick="alert('You clicked a link.'); return false;">Click Me!</a>

Or, better:

<a href="#" id="myLink">Click Me!</a>

<script type="text/javascript">
window.onload = function() {
   document.getElementById('myLink').onclick = function(event) {
      alert('You clicked a link.');
      return false;
   };
};
</script>

The best approach nowadays is to call the proper method of the event property:

<a href="#" id="myLink">Click Me!</a>

<script type="text/javascript">
window.onload = function() {
   document.getElementById('myLink').onclick = function(event) {
      alert('You clicked a link.');
      event.preventDefault(); // <---
   };
};
</script>

It's also best to replace that # with an URI to some proper page, for people not using JavaScript. In some jurisdictions, accessibility is in fact a legal requirement.

Edit Fixed for bleedin' IE:

function f() {
   document.getElementById('myLink').onclick = function(e) {
      alert('You clicked a link.');

    if (!e) {
       var e = window.event;
    }

    // e.cancelBubble is supported by IE - this will kill the bubbling process.
    e.cancelBubble = true;
    e.returnValue  = false;

    // e.stopPropagation works only in Firefox.
    if (e.stopPropagation) {
        e.stopPropagation();
        e.preventDefault();
    }
};

window.onload = f;
凤舞天涯 2024-11-07 05:28:53

诀窍是在事件处理程序上return false

<a href="" onclick="alert('You clicked a link.'); return false">Click Me!</a>

The trick is return false on the event handler.

<a href="" onclick="alert('You clicked a link.'); return false">Click Me!</a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文