HTML
<button>
element
represents a clickable button, used to submit
forms
or anywhere in a document for accessible, standard button functionality.
By default, HTML buttons are presented in a style resembling the platform the
用户代理
runs on, but you can change buttons’ appearance with
CSS
.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
| 内容类别 | 流内容 , 措词内容 , Interactive content , listed , labelable ,和 submittable form-associated element, palpable content. |
|---|---|
| 准许内容 | 措词内容 but there must be no Interactive content |
| Tag omission | None, both the starting and ending tag are mandatory. |
| Permitted parents | Any element that accepts 措词内容 . |
| Implicit ARIA role |
button
|
| Permitted ARIA roles |
checkbox
,
link
,
menuitem
,
menuitemcheckbox
,
menuitemradio
,
option
,
radio
,
switch
,
tab
|
| DOM 接口 |
HTMLButtonElement
|
This element's attributes include the 全局属性 .
autofocus
HTML5
autocomplete
<button>
is nonstandard and Firefox-specific. Unlike other browsers,
Firefox persists the dynamic disabled state
的
<button>
across page loads. Setting
autocomplete="off"
on the button disables this feature; see
bug 654072
.
被禁用
This Boolean attribute prevents the user from interacting with the button: it cannot be pressed or focused.
Firefox, unlike other browsers,
persists the dynamic disabled state
的
<button>
across page loads. Use the
autocomplete
attribute to control this feature.
form
HTML5
<form>
element to associate the button with (its
form owner
). The value of this attribute must be the
id
的
<form>
in the same document. (If this attribute is not set, the
<button>
is associated with its ancestor
<form>
element, if any.)
<button>
elements to
<form>
s anywhere in the document, not just inside a
<form>
. It can also override an ancestor
<form>
元素。
formaction
HTML5
action
attribute of the button's form owner. Does nothing if there is no form owner.
formenctype
HTML5
<form>
and doesn't have
type="button"
), specifies how to encode the form data that is submitted. Possible values:
application/x-www-form-urlencoded
: The default if the attribute is not used.
multipart/form-data
: Use to submit
<input>
elements with their
type
attributes set to
file
.
text/plain
: Specified as a debugging aid; shouldn’t be used for real form submission.
If this attribute is specified, it overrides the
enctype
attribute of the button's form owner.
formmethod
HTML5
<form>
and doesn't have
type="button"
), this attribute specifies the
HTTP method
used to submit the form. Possible values:
post
: The data from the form are included in the body of the HTTP request when sent to the server. Use when the form contains information that shouldn’t be public, like login credentials.
get
: The form data are appended to the form's
action
URL, with a
?
as a separator, and the resulting URL is sent to the server. Use this method when the form
has no side effects
, like search forms.
If specified, this attribute overrides the
method
attribute of the button's form owner.
formnovalidate
HTML5
novalidate
attribute of the button's form owner.
<input type="image">
and
<input type="submit">
元素。
formtarget
HTML5
名称
of, or keyword for, a
浏览上下文
(a tab, window, or
<iframe>
). If this attribute is specified, it overrides the
target
attribute of the button's form owner. The following keywords have special meanings:
_self
: Load the response into the same browsing context as the current one. This is the default if the attribute is not specified.
_blank
: Load the response into a new unnamed browsing context — usually a new tab or window, depending on the user’s browser settings.
_parent
: Load the response into the parent browsing context of the current one. If there is no parent, this option behaves the same way as
_self
.
_top
: Load the response into the top-level browsing context (that is, the browsing context that is an ancestor of the current one, and has no parent). If there is no parent, this option behaves the same way as
_self
.
名称
value
as part of the form data.
type
submit
: The button submits the form data to the server. This is the default if the attribute is not specified for buttons associated with a
<form>
, or if the attribute is an empty or invalid value.
reset
: The button resets all the controls to their initial values, like
<input type="reset">
. (This behavior tends to annoy users.)
button
: The button has no default behavior, and does nothing when pressed by default. It can have client-side scripts listen to the element's events, which are triggered when the events occur.
value
名称
when it’s submitted with the form data. This value is passed to the server in params when the form is submitted.
A submit button with the attribute
formaction
set, but without an associated form does nothing. You have to set a form owner, either by wrapping it in a
<form>
or set the attribute
form
to the id of the form.
<button>
elements are much easier to style than
<input>
elements. You can add inner HTML content (think
<i>
,
<br>
,或者甚至
<img>
), and use
::after
and
::before
pseudo-elements for complex rendering.
If your buttons are not for submitting form data to a server, be sure to set their
type
属性到
button
. Otherwise they will try to submit form data and to load the (nonexistent) response, possibly destroying the current state of the document.
<button name="button">Press me</button>
Buttons that only show an icon to represent do not have an accessible name . Accessible names provide information for assistive technology, such as screen readers, to access when they parse the document and generate an accessibility tree . Assistive technology then uses the accessibility tree to navigate and manipulate page content.
To give an icon button an accessible name, put text in the
<button>
element that concisely describes the button's functionality.
<button name="favorite"> <svg aria-hidden="true" viewBox="0 0 10 10"><path d="M7 9L5 8 3 9V6L1 4h3l1-3 1 3h3L7 6z"/></svg> Add to favorites </button>
If you want to visually hide the button's text, an accessible way to do so is to use a combination of CSS properties to remove it visually from the screen, but keep it parseable by assistive technology.
However, it is worth noting that leaving the button text visually apparent can aid people who may not be familiar with the icon's meaning or understand the button's purpose. This is especially relevant for people who are not technologically sophisticated, or who may have different cultural interpretations for the icon the button uses.
Interactive elements such as buttons should provide an area large enough that it is easy to activate them. This helps a variety of people, including people with motor control issues and people using non-precise forms of input such as a stylus or fingers. A minimum interactive size of 44×44 CSS pixels is recommended.
Large amounts of interactive content — including buttons — placed in close visual proximity to each other should have space separating them. This spacing is beneficial for people who are experiencing motor control issues, who may accidentally activate the wrong interactive content.
Spacing may be created using CSS properties such as
margin
.
Firefox will add a small dotted border on a focused button. This border is declared through CSS in the browser stylesheet, but you can override it to add your own focused style using
button::-moz-focus-inner { }
.
If overridden, it is important to ensure that the state change when focus is moved to the button is high enough that people experiencing low vision conditions will be able to perceive it.
Color contrast ratio is determined by comparing the luminosity of the button text and background color values compared to the background the button is placed on. In order to meet current
Web Content Accessibility Guidelines (WCAG)
, a ratio of 4.5:1 is required for text content and 3:1 for large text. (Large text is defined as 18.66px and
bold
or larger, or 24px or larger.)
Whether clicking on a
<button>
causes it to (by default) become focused varies by browser and OS. The results for
<input>
of
type="button"
and
type="submit"
are the same.
| Desktop Browsers | Windows 8.1 | OS X 10.X |
|---|---|---|
| Firefox | Yes - Firefox 30.0 |
No (even with a
tabindex
) Firefox 63
|
| Chrome | Yes - Chrome 35 | Yes - Chrome 65 |
| Safari | N/A |
No (even with a
tabindex
) Safari 12 (
bug 22261
)
|
| Internet Explorer | Yes - Internet Explorer 11 | N/A |
| Presto | Yes - Opera 12 | Yes - Opera 12 |
| Mobile Browsers | iOS 7.1.2 | Android 4.4.4 |
|---|---|---|
| Safari Mobile |
No (even with a
tabindex
)
|
N/A |
| Chrome 35 |
No (even with a
tabindex
)
|
Yes |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
button
|
Chrome 完整支持 Yes | Edge 完整支持 12 | Firefox 完整支持 Yes | IE 完整支持 Yes | Opera 完整支持 Yes | Safari 完整支持 Yes | WebView Android 完整支持 Yes | Chrome Android 完整支持 Yes | Firefox Android 完整支持 Yes | Opera Android 完整支持 Yes | Safari iOS 完整支持 Yes | Samsung Internet Android 完整支持 Yes |
autocomplete
非标
|
Chrome 不支持 No | Edge 不支持 No | Firefox 完整支持 Yes | IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 完整支持 Yes | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
autofocus
|
Chrome 完整支持 5 | Edge 完整支持 12 | Firefox 完整支持 4 | IE 完整支持 10 | Opera 完整支持 9.6 | Safari 完整支持 5 | WebView Android 完整支持 37 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android ? | Safari iOS ? | Samsung Internet Android 完整支持 1.0 |
被禁用
|
Chrome 完整支持 Yes | Edge 完整支持 12 | Firefox 完整支持 Yes | IE 完整支持 Yes | Opera 完整支持 Yes | Safari 完整支持 Yes | WebView Android 完整支持 Yes | Chrome Android 完整支持 Yes | Firefox Android 完整支持 Yes | Opera Android 完整支持 Yes | Safari iOS 完整支持 Yes | Samsung Internet Android 完整支持 Yes |
form
|
Chrome 完整支持 Yes | Edge 完整支持 16 | Firefox 完整支持 Yes | IE 不支持 No | Opera 完整支持 Yes | Safari 完整支持 Yes | WebView Android 完整支持 Yes | Chrome Android 完整支持 Yes | Firefox Android 完整支持 Yes | Opera Android 完整支持 Yes | Safari iOS 完整支持 Yes | Samsung Internet Android 完整支持 Yes |
formaction
|
Chrome 完整支持 9 | Edge 完整支持 12 | Firefox 完整支持 4 | IE 完整支持 10 | Opera 完整支持 15 | Safari 完整支持 5.1 | WebView Android 完整支持 37 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android ? | Safari iOS 完整支持 5 | Samsung Internet Android 完整支持 1.0 |
formenctype
|
Chrome 完整支持 9 | Edge 完整支持 12 | Firefox 完整支持 4 | IE 完整支持 10 | Opera 完整支持 10.6 | Safari ? | WebView Android 完整支持 37 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android ? | Safari iOS ? | Samsung Internet Android 完整支持 1.0 |
formmethod
|
Chrome 完整支持 9 | Edge 完整支持 12 | Firefox 完整支持 4 | IE 完整支持 10 | Opera 完整支持 15 | Safari ? | WebView Android 完整支持 37 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android ? | Safari iOS ? | Samsung Internet Android 完整支持 1.0 |
formnovalidate
|
Chrome 完整支持 Yes | Edge 完整支持 12 | Firefox 完整支持 Yes | IE 完整支持 Yes | Opera 完整支持 Yes | Safari 完整支持 Yes | WebView Android 完整支持 Yes | Chrome Android 完整支持 Yes | Firefox Android 完整支持 Yes | Opera Android 完整支持 Yes | Safari iOS 完整支持 Yes | Samsung Internet Android 完整支持 Yes |
formtarget
|
Chrome 完整支持 Yes | Edge 完整支持 12 | Firefox 完整支持 Yes | IE 完整支持 Yes | Opera 完整支持 Yes | Safari 完整支持 Yes | WebView Android 完整支持 Yes | Chrome Android 完整支持 Yes | Firefox Android 完整支持 Yes | Opera Android 完整支持 Yes | Safari iOS 完整支持 Yes | Samsung Internet Android 完整支持 Yes |
名称
|
Chrome 完整支持 Yes | Edge 完整支持 12 | Firefox 完整支持 Yes | IE 完整支持 Yes | Opera 完整支持 Yes | Safari 完整支持 Yes | WebView Android 完整支持 Yes | Chrome Android 完整支持 Yes | Firefox Android 完整支持 Yes | Opera Android 完整支持 Yes | Safari iOS 完整支持 Yes | Samsung Internet Android 完整支持 Yes |
type
|
Chrome 完整支持 Yes | Edge 完整支持 12 | Firefox 完整支持 Yes | IE 完整支持 Yes | Opera 完整支持 Yes | Safari 完整支持 Yes | WebView Android 完整支持 Yes | Chrome Android 完整支持 Yes | Firefox Android 完整支持 Yes | Opera Android 完整支持 Yes | Safari iOS 完整支持 Yes | Samsung Internet Android 完整支持 Yes |
value
|
Chrome 完整支持 Yes | Edge 完整支持 12 | Firefox 完整支持 Yes | IE 完整支持 Yes | Opera 完整支持 Yes | Safari 完整支持 Yes | WebView Android 完整支持 Yes | Chrome Android 完整支持 Yes | Firefox Android 完整支持 Yes | Opera Android 完整支持 Yes | Safari iOS 完整支持 Yes | Samsung Internet Android 完整支持 Yes |
完整支持
不支持
兼容性未知
非标。预期跨浏览器支持较差。
<button>
<datalist>
<fieldset>
<form>
<input>
<label>
<legend>
<meter>
<optgroup>
<option>
<output>
<progress>
<select>
<textarea>