对于 jQuery,href 中的 # 是多么必要
有时,当您运行一段 jQuery 时,您会在 HREF 属性中放置一个 # 符号,但为什么呢?我确实明白 # 的含义,但我问你是否有这个 Run
为什么你实际上要放一个 #在 href 中并返回 false,您可以将其留空并获得相同的结果吗?
Sometimes when you run a piece of jQuery, you put a # symbol in the HREF attribute, but why? I do understand what # means, but i'm asking if you have this <a href="#" id="runScript">Run</a>
why would you actually put a # in the href and return false, could you just leave it blank and achieve the same results?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
正如上面的帖子所说,它不会被设计为可点击的链接。但你可以
在 CSS 中这样做:
As the above posts said, it won't be styled as a clickable link. But instead you can just do:
in the CSS.
这两种变体都是邪恶的。例如,它们因“在新选项卡中打开”而失败。您应该使用#enoughInfoToLoadThePage 来代替。或者也许是一个无需 JavaScript 即可工作的 url。
您甚至可能想使用此技术来使它们可供抓取通过谷歌。
Both variants of this are evil. For example they fail for "Open in new Tab". You should use
#enoughInfoToLoadThePage
instead. Or perhaps an url which works without javascript.And you might even want to use this technique to make them crawlable by google.
如果将 href 保留为“”(空白),则您所在的同一页面将再次加载该页面。
return false 或 # 确保页面未加载。
if you leave href as "" (blank), the same page which you are on will load the page again.
return false or # makes sure the page is not loaded .
另一件需要注意的事情是,如果您没有 href 属性,那么在视觉上就不会像普通链接一样。请参阅此示例。
Another thing to note is that if you have no href attribute the won't visually act like a normal link. See this example.
如果浏览器上没有 javascript,单击链接将不会加载新页面。
If there is no javascript on the browser, clicking the link will not load a new page.
如果将其留空,某些奇怪的浏览器可能不会将
设置为可点击链接。这可能是我们需要
#
的唯一原因。然而,使用
href
更好的做法是链接到无需脚本即可实现相同功能的页面,例如新项目
If you leave it blank, some quirk browser might not style the
<a>
as a click-able link. That's probably the only reason we need that#
.A better thing to do with
href
however is link to a page that achieve the same function w/o script, e.g.<a href="/new" id="load-new-btn">New Items</a>
将
href
属性留空将无法实现与停止默认操作相同的效果。空白值可能会被浏览器视为非值,导致它将元素呈现为书签而不是链接,或者作为同一页面的链接,导致重新加载。值
#
只是一个 URL,如果脚本无法抑制默认操作,它会造成最小的干扰。如果脚本以错误结束,默认操作仍会启动。Leaving the
href
attribute blank will not accomplish the same as stopping the default action. A blank value may either be percieved by the browser as a non-value, causing it to render the element as a bookmark instead of a link, or as a link to the same page, causing a reload.The value
#
is just an URL that causes a minimum of disturbance if the script fails to suppress the default action. If the script ends with an error, the default action will still kick in.