<a> vs </a> <按钮> <a> vs </a> <DIV> <a> vs <input type="button&#34;&gt;

时间:2016-05-10 19:58:43

标签: html html5

<p>I" ve="" seen="" so="" many="" posts="" along="" the="" lines="" of="" "i'd="" like="" to="" one-specific-thing for my one-specific-situation when it comes to buttons in HTML.

If I may, I'd like to get a professional take all in one post, here:

When making buttons in HTML, what situations lead to:

  • <a> being the best answer?
  • <button> being the best answer?
  • <div> being the best answer?
  • <input type='button'> being the best answer?

2 个答案:

答案 0 :(得分:24)

good article可以很好地总结差异

简而言之:

|                       |       General usage        | Complex designs | Can be disabled |
|-----------------------|----------------------------|-----------------|-----------------|
| <button>              | General button purpose.    | Yes             | Yes             |
|                       | Used in most cases         |                 |                 |
| --------------------- | -------------------------- | --------------- | --------------- |
| <input type="button"> | Usually used inside        | No              | Yes             |
|                       | forms                      |                 |                 |
| --------------------- | -------------------------- | --------------- | --------------- |
| <a>                   | Used to navigate to        | Yes             | No              |
|                       | pages and resources        |                 |                 |
| --------------------- | -------------------------- | --------------- | --------------- |
| <div>                 | Can be used for clickable  | Yes             | No              |
|                       | area which is not button   |                 |                 |
|                       | or link by definition      |                 |                 |

答案 1 :(得分:14)

通常,我尝试以最常用的语义方式使用HTML标记。有用的方式:

  • <a>标记:a用于&#34;锚定#34;,当按钮是指向页面上内容或外部内容的链接时,我会使用它;
  • div tag:这是一个简单的容器。我使用它时,我必须做简单的按钮,没有任何特定的工作,但真正的自定义风格(简单的动画,过渡,开放模式等)。这是快活的。
  • <button>标签:根据W3,这是在页面上创建按钮的标准标签,因此在需要像onClick()这样的动作时使用它。
  • <input type="button">标记:等于按钮标记,但您需要它来表单提交,因为使用<button>提交的浏览器可以提交不同的值。我一般在表格上使用它。