简体   繁体   English

什么是<button onclick=…>之间</button>的区别

[英]what is the difference between <button onclick=…> and <a href=“#” onclick=…/>

I find many times the pages use "a" tag and want to make it like a button. 我发现很多次页面都使用“a”标签,并希望将其设为按钮。 it's something like this: 它是这样的:

<a href="#" class="button" onclick="...." />

I'm confusing about why not just use "button" tag? 我很困惑为什么不只是使用“按钮”标签? like this: 像这样:

<button onclick="....">button</button>

what is the difference between? 有什么区别? I really want to learn it, thanks! 我真的想学习它,谢谢!

One more situation question: 还有一个情况问题:

Three "button-like < a > tag" as below: 三个“按钮式<a>标签”如下:

在此输入图像描述

Hint: 暗示:

  • Different one call ajax then get different period record 不同的人打电话给ajax然后得到不同的期间记录
  • Need to use onclick="location.replace()" because back to last page smoothly. 需要使用onclick =“location.replace()”因为顺利回到最后一页。

Original code: 原始代码:

<a href="url" class="btn" >Today</a> 

I have changed to: 我改为:

<a href="#" onclick="location.replace(url)" class="btn" >Today</a> 

Consider about: 考虑关于:

<button onclick="location.replace(url)">Today</button>

What will you do in this situation? 在这种情况下你会做什么? Is any incorrect to use button tag ? 使用按钮标签有什么不对吗?

So the answer here is: semantics. 所以这里的答案是:语义。

An anchor should be used for a hyperlink. 锚点应该用于超链接。 Navigational items used to move from one page to another. 用于从一个页面移动到另一个页面的导航项目。 It's a reference to data that the user can get to by clicking the link. 它是用户可以通过单击链接获得的数据的引用。

A button is a button. 按钮是一个按钮。 It's used for functionality. 它用于功能。

In your example they're both calling an onclick event, so they're only going to have one difference. 在你的例子中,他们都在调用onclick事件,所以他们只会有一个区别。 In the case of the anchor you have to override the default behavior using event.preventDefault() . 对于锚点,您必须使用event.preventDefault()覆盖默认行为。

Also, the top 3 results via Google: 此外,谷歌的前3名成绩:

http://davidwalsh.name/html5-buttons http://davidwalsh.name/html5-buttons

HTML/CSS - Buttons - When to use what HTML / CSS - 按钮 - 何时使用什么

http://www.reddit.com/r/Frontend/comments/1y9zdh/anchor_vs_button_a_question_on_html_semantics/ http://www.reddit.com/r/Frontend/comments/1y9zdh/anchor_vs_button_a_question_on_html_semantics/

This is basically a historical artifact. 这基本上是一件历史文物。

It stems from the time when it was much easier to apply custom styling to an anchor. 它源于将定制样式应用于锚点更容易的时间。 You could easily build auto-sized "button-looking" anchors by using more elements inside the anchor itself. 您可以通过在锚本身内使用更多元素轻松构建自动调整大小的“按钮外观”锚点。

Today, with enhanced CSS options and better browser compatibility, this is not necessary. 今天,通过增强的CSS选项和更好的浏览器兼容性,这不是必需的。 When button is the correct semantic element, you have no reason to use a instead. button是正确的语义元素,你没有理由使用a替代。

So, use anchors for links to other pages. 因此,使用锚点来链接到其他页面。 It should always have a href , rather than just using # and onclick . 它应该总是有一个href ,而不是只使用#onclick Now, you can still use onclick as well - just make sure that the href directs you to the same data that onclick does. 现在,您仍然可以使用onclick - 只需确保href将您引导至与onclick相同的数据。 This is very handy when you want to have a way for search bots to navigate your site, even though the actual users will be presented with eg a more responsive interface (for example, downloading the updated content through AJAX). 当您希望为搜索机器人导航您的站点时,这非常方便,即使实际用户将被呈现例如更具响应性的界面(例如,通过AJAX下载更新的内容)。 Make sure that common methods of opening the link in a new window / tab still work (neither of ctrl+click, right-click and middle-click should execute the onclick action). 确保在新窗口/选项卡中打开链接的常用方法仍然有效(ctrl + click,右键单击和中键单击都不应执行onclick操作)。

Buttons are the elements that are there to interact with the page you're currently on, whether that means doing client-side scripting, or sending a form to the server. 按钮是与您当前所在页面交互的元素,无论是执行客户端脚本还是将表单发送到服务器。

EDIT : 编辑

With the edit to your question, it's obvious you should simply use an anchor with a normal href . 通过编辑您的问题,很明显您应该只使用具有正常href的锚点。 There's no reason to use either onclick or a button , and you are just making a simple hyperlink, that's what anchors are for. 我们没有理由为使用onclickbutton ,而只是制作一个简单的超链接,这就是锚的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 之间有什么区别<a href>,</a><div onclick> <a href>, 和<button>?</button></a> - What is the difference between <a href>, <div onclick>, and <button>? onclick和href =“javascript:函数名称有什么区别? - What is the Difference between onclick and href="javascript:function name? onClick={tmp} 和 onClick={()=&gt;tmp} 有什么区别 - What is the difference between onClick={tmp} and onClick={()=>tmp} 将<a href=…>onclick</a>转换<a href=…>为按钮</a> - Converting an <a href=…> to a button onclick “onClick = { () =&gt; this.props.onClick() } 和 onClick = {props.onClick } 有什么区别? - What is the difference between "onClick = { () => this.props.onClick() } and onClick = {props.onClick }? onClick = {onRouteChange(&#39;home&#39;)}和onClick = {()=&gt; onRouteChange(&#39;home&#39;)}有什么区别? - What is the difference between onClick={onRouteChange('home')} and onClick={ () => onRouteChange('home')}? onClick =“ javascript:function(&#39;value&#39;)&#39;”和onClick =“” function(&#39;value&#39;);“有什么区别? - What is the difference between onClick=“javascript: function('value')'” and onClick=“function('value');”? 什么之间的区别<a onclick=“someFunction”>和</a> - what's the difference between <a onclick=“someFunction”> and <a onclick=“someFunction()”> React JSX 中的 onClick={increase} 和 onClick={()=&gt;{increase()}} 有什么区别? - What is the difference between onClick={increase} and onClick={()=>{increase()}} in React JSX? onClick ={ () =&gt; function()} 和 onClick = {function()} 有什么区别? - What's the difference between onClick ={ () => function()} and onClick = {function()}?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM