< button type =" button"> ;?的重点是什么? [英] What's the point of <button type="button">?
问题描述
< button type =button>
与简单的< button>
有任何不同或者缺少类型
属性?MDN和HTML5规范说 type = button
用于触发自定义的按钮JavaScript,但是,默认情况下,< button>
是什么呢?
Is <button type="button>
any different from a simple <button>
with a blank or missing type
attribute? MDN and the HTML5 spec say that type=button
is for buttons whose purpose is to trigger custom JavaScript, but isn't that also what a <button>
does by default?
推荐答案
是的,这是有原因的 - 但(通常)只有当你在一个< form>
元素时。
Yep, there's a reason - but (usually) only if you're in a <form>
element.
如果在表单元素中包含一个按钮而没有指定它只是一个常规按钮,它默认为一个提交按钮。
If you include a button in a form element without specifying it's just a regular button, it defaults to a submit button.
<form>
<button>I will submit the form when clicked!</button>
</form>
vs
<form>
<button type='button'>I won't!</button>
</form>
假设第一个是 type = submit
,因为尚未指定类型
属性。
The first one is assumed to be type=submit
since a type
attribute hasn't been specified.
如果您在< form>
元素中不,则按钮赢了没有什么可以提交的,所以没那么重要。 :)
If you are not in a <form>
element, the button won't have anything to submit, so it doesn't matter as much. :)
语义通常在应用程序生命周期的某个时刻变得很重要,所以养成指定类型的习惯是个好主意
。
Semantics usually become important at some point in your application's lifetime, though, so it's a good idea to make a habit of specifying the type
.
唯一可能相关的原因是,是否存在指定样式规则 [type = button]
或其他什么。但不建议这样做。
The only other reason that could be relevant is if there's a styling rule that specifies [type=button]
or something. That's not recommended, though.
这篇关于< button type =" button"> ;?的重点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!