输入类型=“提交” VS 按钮标签可以互换吗?

发布于 2024-11-30 18:03:28 字数 186 浏览 1 评论 0 原文

input type="submit"button 标签可以互换吗?或者如果有任何区别,那么何时使用 input type="submit" 以及何时使用 button

如果没有区别,那么为什么我们有 2 个用于相同目的的标签?

input type="submit" and button tag are they interchangeable? or if there is any difference then When to use input type="submit" and when button ?

And if there is no difference then why we have 2 tags for same purpose?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

稍尽春風 2024-12-07 18:03:28

http://www.w3.org/TR/html4/interact /forms.html#h-17.5

使用 BUTTON 元素创建的按钮功能就像使用 INPUT 元素创建的按钮一样,但它们提供了更丰富的渲染可能性:BUTTON 元素可能有内容。例如,包含图像功能的 BUTTON 元素可能类似于类型设置为“image”的 INPUT 元素,但 BUTTON 元素类型允许内容。

因此,仅就功能而言,它们是可以互换的!

(请注意,type="submit"button 的默认值)

http://www.w3.org/TR/html4/interact/forms.html#h-17.5

Buttons created with the BUTTON element function just like buttons created with the INPUT element, but they offer richer rendering possibilities: the BUTTON element may have content. For example, a BUTTON element that contains an image functions like and may resemble an INPUT element whose type is set to "image", but the BUTTON element type allows content.

So for functionality only they're interchangeable!

(Note, type="submit" is the default for button)

幻想少年梦 2024-12-07 18:03:28

尽管这两个元素功能上提供相同的结果*,我强烈建议您使用

  • 更加明确和可读。 input 表明该控件可编辑,或者可以由用户编辑; button 就其用途而言更加明确 更
  • 容易在 CSS 中设置样式;如上所述,FIrefox 和 IE 有一些怪癖,其中 input[type="submit"] 在某些情况下无法正确显示
  • 可预测的请求:当在 POST 中提交值时,IE 会出现非常奇怪的行为/GET 向服务器发出
  • 标记友好的请求;您可以在按钮内嵌套项目,例如图标。
  • HTML5,前瞻性;作为开发人员,我们有责任在新规范正式发布后采用它。截至目前,HTML5 已经正式推出一年多了,并且在许多情况下都被证明可以提高 SEO

*

总之,我强烈反对使用

Although both elements deliver functionally the same result *, I strongly recommend you use <button>:

  • Far more explicit and readable. input suggests that the control is editable, or can be edited by the user; button is far more explicit in terms of the purpose it serves
  • Easier to style in CSS; as mentioned above, FIrefox and IE have quirks in which input[type="submit"] do not display correctly in some cases
  • Predictable requests: IE has verying behaviours when values are submitted in the POST/GET request to the server
  • Markup-friendly; you can nest items, for example, icons, inside the button.
  • HTML5, forward-thinking; as developers, it is our responsibility to adopt to the new spec once it is officialized. HTML5, as of right now, has been official for over one year now, and has been shown in many cases to boost SEO.

* With the exception of <button type="button"> which by default has no specified behaviour.

In summary, I highly discourage use of <input type="submit"/>.

十年九夏 2024-12-07 18:03:28

只是一个按钮,本身不会执行任何操作。
在表单元素内时,将在单击时提交表单。

另一个有用的“特殊”按钮是 ,它将清除表单。

The <input type="button" /> is just a button and won't do anything by itself.
The <input type="submit" />, when inside a form element, will submit the form when clicked.

Another useful 'special' button is the <input type="reset" /> that will clear the form.

春风十里 2024-12-07 18:03:28

使用<按钮>标签而不是 。这是 bootstrap 3 中建议的做法。

http://getbootstrap.com/css/#buttons-tags< /a>

“跨浏览器渲染

作为最佳实践,我们强烈建议使用<按钮>元素
尽可能确保匹配的跨浏览器渲染。

除其他事项外,Firefox 存在一个错误,它阻止我们
设置基于 的按钮的行高,导致它们不
与 Firefox 上其他按钮的高度完全匹配。”

Use <button> tag instead of <input type="button"..>. It is the advised practice in bootstrap 3.

http://getbootstrap.com/css/#buttons-tags

"Cross-browser rendering

As a best practice, we highly recommend using the <button> element
whenever possible to ensure matching cross-browser rendering.

Among other things, there's a Firefox bug that prevents us from
setting the line-height of <input>-based buttons, causing them to not
exactly match the height of other buttons on Firefox."

巨坚强 2024-12-07 18:03:28

不支持其中的 HTML,因为它是单个自关闭标记。另一方面,

除非您有特殊原因,否则最好坚持使用

<input type='submit' /> doesn't support HTML inside of it, since it's a single self-closing tag. <button>, on the other hand, supports HTML, images, etc. inside because it's a tag pair: <button><img src='myimage.gif' /></button>. <button> is also more flexible when it comes to CSS styling.

The disadvantage of <button> is that it's not fully supported by older browsers. IE6/7, for example, don't display it correctly.

Unless you have some specific reason, it's probably best to stick to <input type='submit' />.

如果没有你 2024-12-07 18:03:28

我意识到这是一个老问题,但我在 mozilla.org 上发现了这个问题,并认为它适用。

按钮可以分为三种类型:提交、重置或按钮。单击一个
提交按钮将表单的数据发送到由
元素的动作属性。

单击重置按钮
立即将所有表单小部件重置为其默认值。来自一个
从用户体验的角度来看,这被认为是不好的做法。

单击按钮
按钮...什么也没做!这听起来很傻,但是却非常有用
使用 JavaScript 构建自定义按钮。

https://developer.mozilla.org/en- US/docs/Web/Guide/HTML/Forms/My_first_HTML_form#And_a_

I realize this is an old question but I found this on mozilla.org and think it applies.

A button can be of three types: submit, reset, or button. A click on a
submit button sends the form's data to the web page defined by the
action attribute of the element.

A click on a reset button
resets all the form widgets to their default value immediately. From a
UX point of view, this is considered bad practice.

A click on a button
button does... nothing! That sounds silly, but it's amazingly useful
to build custom buttons with JavaScript.

https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/My_first_HTML_form#And_a_<button>_to_finish

吐个泡泡 2024-12-07 18:03:28

<button> is newer than <input type="submit">, is more semantic, easy to stylize and support HTML inside of it.

小霸王臭丫头 2024-12-07 18:03:28

虽然其他答案都很好并且回答了问题,但在使用 input type="submit"button 时需要考虑一件事。使用 input type="submit",您不能在输入上使用 CSS 伪元素,但您可以获取一个按钮!

这是在样式设置时在输入上使用 button 元素的原因之一。

While the other answers are great and answer the question there is one thing to consider when using input type="submit" and button. With an input type="submit" you cannot use a CSS pseudo element on the input but you can for a button!

This is one reason to use a button element over an input when it comes to styling.

追星践月 2024-12-07 18:03:28

我不知道这是一个错误还是一个功能,但我发现了一个非常重要的(至少在某些情况下)差异: 创建键值在您的请求中配对,而

因此,当您的表单中有多个提交按钮并且想知道单击了哪一个时 - 不要使用 button,而是使用 input type="submit"

I don't know if this is a bug or a feature, but there is very important (for some cases at least) difference I found: <input type="submit"> creates key value pair in your request and <button type="submit"> doesn't. Tested in Chrome and Safari.

So when you have multiple submit buttons in your form and want to know which one was clicked - do not use button, use input type="submit" instead.

生生漫 2024-12-07 18:03:28

它不会自动提交表单

如果您正在谈论 ,如果您正在谈论

最重要的是,如果您希望在所有浏览器中单击时提交表单,请使用

If you are talking about <input type=button>, it won't automatically submit the form

if you are talking about the <button> tag, that's newer and doesn't automatically submit in all browsers.

Bottom line, if you want the form to submit on click in all browsers, use <input type="submit">

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文