TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: Href=“#” VS href=“JavaScript:;”

2 点作者 optikals将近 8 年前
Sorry for my silly question, can you tell me if there&#x27;s a difference between href=&quot;#!&quot; and href=&quot;javascript:;&quot;<p>and which one it&#x27;s better to use on the links with JavaScript attached to them (nothing else, JS only).<p>I know it&#x27;s not the correct use, it should be a &quot;button&quot;, but I really need to use &lt;a&gt;.<p>Thank you.

2 条评论

twobyfour将近 8 年前
Neither, put a real URL in there so people can a) use your site without JS and b) open links in new tabs. Use e.preventDefault().<p>If you can&#x27;t put a URL in it, use a different element. Why can&#x27;t you use &lt;button&gt;? It&#x27;s definitely the correct element for something that takes an action. In a pinch, fall back on &lt;span&gt;?
评论 #14748108 未加载
savethefuture将近 8 年前
There is no &quot;correct&quot; way to do anything in web, I would say its generally best practice or personal preference. I tend to use # and target the tag with a data attribute and use e.preventDefault(). #! from what I&#x27;ve seen is used for handling client-side routing. Any tag can be used as a button in javascript, as long as you target it correctly.
评论 #14706376 未加载