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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Why using anchors as buttons sucks

50 点作者 tkazec超过 13 年前

7 条评论

DanielBMarkham超过 13 年前
I hate to be critical, and maybe I'm missing something, but this essay didn't read well at all. Sounded like somebody who didn't fully understand what they were doing thrashing around.<p>We use anchor tags as buttons any more because clicking on buttons should (eventually) change the href.location. That means, yes, right-click should work just fine, opening up a new window with your action applied to the current state. Don't look at buttons like Javascript events. Rather look at them like code that changes the URI that then kicks off Javascript events. It's a subtle difference, but it's very important. An anchor as a button isn't the same as a button because they're two completely different things. Your mental model of each should be different.<p>I don't see where there's much of a mystery or rant here, but maybe I missed it.
评论 #3563929 未加载
tikhonj超过 13 年前
Actual buttons annoy me. I like navigating around using just the keyboard, so a lot of the time I just do a search for the link I want to click (ctrl-f on Chrome or ' on Firefox) then just use enter to click the link. This works great when the button is actually just a styled link, but does not work at all with an actual button.<p>Of course, I'm indubitably in a tiny minority, but it's still something to note.
评论 #3553760 未加载
评论 #3553818 未加载
评论 #3554298 未加载
评论 #3553883 未加载
评论 #3553845 未加载
评论 #3553752 未加载
yaix超过 13 年前
Why don't you add href='#' and return false from the click event. Nothing gets added to the history that way.<p>Better yet, add a href='/uri/to/script' as a fallback in case the user has JS disabled.
评论 #3553680 未加载
评论 #3554089 未加载
评论 #3553944 未加载
talmand超过 13 年前
For a moment there I was trying really hard to understand what he was complaining about. His post made very little sense to me as it was written. I guess because I haven't even come close to experiencing his "problem".<p>Especially since I looked over the pull request he mentions and the explanation as to why href="#" is used is right there in front of his face.<p>Plus, what's with all the hate for href="#"? Are some people so anal about their code they can't leave one simple little thing in place that makes life easier for them?
评论 #3563526 未加载
mattmanser超过 13 年前
You missed not being able to disable them without javascript if you set the href, pretty big disadvantage to miss. I vaguely remember IE might support the disabled attribute, but it doesn't actually work properly. That might just be 6 though. I also have a feeling there are other weird side effects to not setting a href attribute on an anchor, but maybe it's just that.<p>This doesn't feel a very comprehensive or well researched post, especially as you didn't know about e.preventDefault() which afaik is standard practice these days, seems a fairly ballsy thing to ask bootstrap to do when you don't really seem to know much about it.<p>Then again I guess if you don't question these things, you never find out!
评论 #3563561 未加载
Achshar超过 13 年前
here's what i do, i use button tag where it has to look like a button and use a link where it has to look like a link. Also if link requires an eventhandler instead of href change i simply omit href attribute altogether and write my css to apply to a tag irrespective of weather it has href attribute or not. problem solved.
评论 #3554493 未加载
voidr超过 13 年前
You create an anchor like you would, you write a proper onClick handler that does your favorite animation and then sets the window.location to the href or if you don't want to figure this out, you could just use something like jQuery UI button and you are happy.