在 javascript 目标链接上继续使用 href='#' 有什么特别好的理由吗? [英] Is there any particular good reason to keep using href='#' on javascript targeted links?

查看:23
本文介绍了在 javascript 目标链接上继续使用 href='#' 有什么特别好的理由吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,这样的代码非常常见:

As you all know, it's extremely common to have code like this:

<a href='#' class='edit'>Edit</a>

然后连接到事件处理程序(使用 jQuery 或最近流行的任何东西)

Which is then hooked up to an event handler (using jQuery or whatever's hip these days)

甚至使用内联 js 处理程序

or even with inline js handlers

<a href='#' onclick='editThis()'>Edit</a>

我知道在链接上显示指针/手形光标是一种懒惰的方式,但只需使用此 css 即可轻松纠正:

I know it's a lazy way to show the pointer/hand cursor on the link but that can easily be corrected with just this css:

  a {
       cursor:pointer;
  }

(在样式表中写入一次比在每个链接上写入 href='#' 花费的时间更少)

如果由于某种原因处理程序没有机会将自己附加到元素(鼠标触发快乐用户,不耐烦类型,...)

Having href='#' also has the annoying inconvenience of causing the browser to jump to the top of the page if for some reason the handler hasn't had a chance to attach itself to the element (mouse trigger happy user, the impatient type, ...)

我的问题是,是否有特别好的理由继续使用 href='#' ?

My question, is there a particular good reason to keep using the href='#' ?

删除它会破坏某些特定上下文中的某些浏览器行为吗?

Would removing it break some browser behaviour in a few particular contexts?

编辑:对于这个问题,我得到了一些非常随机的答案,也许我应该澄清一下.

EDIT: I'm getting some really random answer for this question, maybe I should clarify.

问题是:如果我通过 css 设置手形光标(以及下划线和颜色 - 感谢 Borealid),我可以完全去掉 href 属性吗

这个问题不是关于什么是最好的 href 或者应该如何附加事件处理程序

This question is NOT about what the best href is or how one should attach event handlers

推荐答案

一般来说,我不为 js 调用使用标签,它很草率,并且会给出您正在谈论的响应.如果我正在做一个锚完全使用它,但如果你不只是使用一个类作为你的选择器并在 css 中完成其余的工作.

in general I don't use a tags for js calls, its sloppy and gives the response you're talking about. If I am doing an anchor totally use it, but if you're not just use a class as your selector and do the rest in css.

这篇关于在 javascript 目标链接上继续使用 href='#' 有什么特别好的理由吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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