--- title: slug: Web/HTML/Element/button translation_of: Web/HTML/Element/button --- HTML <button> 元素代表一個可點擊的按鈕。 內容類別 流內容, 分段內容, 互動內容, 條列的, 標籤式的, 以及 可提交式的關係形式元素, 可觸知的內容. 被允許的內容 分段內容. 遺漏標籤 所有標籤皆不可遺漏 允許的父級元素 任何接受分段內容的元素. 可接受 ARIA設定的角色 {{ARIARole("checkbox")}}, {{ARIARole("link")}}, {{ARIARole("menuitem")}}, {{ARIARole("menuitemcheckbox")}}, {{ARIARole("menitemradio")}}, {{ARIARole("radio")}}, {{ARIARole("switch")}}, {{ARIARole("tab")}} DOM介面 {{domxref("HTMLButtonElement")}} 屬性 這元素的屬性包含全域屬性. {{htmlattrdef("autofocus")}} {{HTMLVersionInline(5)}} 頁面載入後, 該按鈕如有輸入的焦點, 這個布林值式屬性即可指定上去, 除非使用者覆寫之, 例如通過不同的控制. 文本中只有一個關聯形式元素可以被指定該屬性. {{htmlattrdef("autocomplete")}} {{non-standard_inline}} The use of this attribute on a {{HTMLElement("button")}} is nonstandard and Firefox-specific. By default, unlike other browsers, Firefox persists the dynamic disabled state of a {{HTMLElement("button")}} across page loads. Setting the value of this attribute to off (i.e. autocomplete="off") disables this feature. See {{bug(654072)}}. {{htmlattrdef("disabled")}} This Boolean attribute indicates that the user cannot interact with the button. If this attribute is not specified, the button inherits its setting from the containing element, for example {{HTMLElement("fieldset")}}; if there is no containing element with the disabled attribute set, then the button is enabled. Firefox will, unlike other browsers, by default, persist the dynamic disabled state of a {{HTMLElement("button")}} across page loads. Use the {{htmlattrxref("autocomplete","button")}} attribute to control this feature. {{htmlattrdef("form")}} {{HTMLVersionInline(5)}} The form element that the button is associated with (its form owner). The value of the attribute must be the id attribute of a {{HTMLElement("form")}} element in the same document. If this attribute is not specified, the <button> element will be associated to an ancestor {{HTMLElement("form")}} element, if one exists. This attribute enables you to associate <button> elements to {{HTMLElement("form")}} elements anywhere within a document, not just as descendants of {{HTMLElement("form")}} elements. {{htmlattrdef("formaction")}} {{HTMLVersionInline(5)}} The URI of a program that processes the information submitted by the button. If specified, it overrides the {{htmlattrxref("action","form")}} attribute of the button's form owner. {{htmlattrdef("formenctype")}} {{HTMLVersionInline(5)}} If the button is a submit button, this attribute specifies the type of content that is used to submit the form to the server. Possible values are: application/x-www-form-urlencoded: The default value if the attribute is not specified. multipart/form-data: Use this value if you are using an {{HTMLElement("input")}} element with the {{htmlattrxref("type","input")}} attribute set to file. text/plain If this attribute is specified, it overrides the {{htmlattrxref("enctype","form")}} attribute of the button's form owner. {{htmlattrdef("formmethod")}} {{HTMLVersionInline(5)}} If the button is a submit button, this attribute specifies the HTTP method that the browser uses to submit the form. Possible values are: post: The data from the form is included in the body of the form and is sent to the server. get: The data from the form are appended to the form attribute URI, with a '?' as a separator, and the resulting URI is sent to the server. Use this method when the form has no side-effects and contains only ASCII characters. If specified, this attribute overrides the {{htmlattrxref("method","form")}} attribute of the button's form owner. {{htmlattrdef("formnovalidate")}} {{HTMLVersionInline(5)}} If the button is a submit button, this Boolean attribute specifies that the form is not to be validated when it is submitted. If this attribute is specified, it overrides the {{htmlattrxref("novalidate","form")}} attribute of the button's form owner. {{htmlattrdef("formtarget")}} {{HTMLVersionInline(5)}} If the button is a submit button, this attribute is a name or keyword indicating where to display the response that is received after submitting the form. This is a name of, or keyword for, a browsing context (for example, tab, window, or inline frame). If this attribute is specified, it overrides the {{htmlattrxref("target", "form")}} 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 value is the default if the attribute is not specified. _blank: Load the response into a new unnamed browsing context. _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. {{htmlattrdef("name")}} The name of the button, which is submitted with the form data. {{htmlattrdef("type")}} The type of the button. Possible values are: submit: The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value. reset: The button resets all the controls to their initial values. button: The button has no default behavior. It can have client-side scripts associated with the element's events, which are triggered when the events occur. menu: The button opens a popup menu defined via its designated {{HTMLElement("menu")}} element. {{htmlattrdef("value")}} The initial value of the button. It defines the value associated with the button which is submitted with the form data. This value is passed to the server in params when the form is submitted. Notes <button> elements are much easier to style than {{HTMLElement("input")}} elements. You can add inner HTML content (think <em>, <strong> or even <img>), and make use of {{Cssxref(":after")}} and {{Cssxref(":before")}} pseudo-element to achieve complex rendering while {{HTMLElement("input")}} only accepts a text value attribute. IE7 has a bug where when submitting a form with <button type="submit" name="myButton" value="foo">Click me</button>, the POST data sent will result in myButton=Click me instead of myButton=foo. IE6 has an even worse bug where submitting a form through a button will submit ALL buttons of the form, with the same bug as IE7. This bug has been fixed in IE8. Firefox will add, for accessibility purposes, a small dotted border on a focused button. This border is declared through CSS, in the browser stylesheet, but you can override it if necessary to add your own focused style using button{{cssxref("::-moz-focus-inner")}} { } Firefox will, unlike other browsers, by default, persist the dynamic disabled state of a {{HTMLElement("button")}} across page loads. Setting the value of the {{htmlattrxref("autocomplete","button")}} attribute to off disables this feature. See {{bug(654072)}}. Firefox <35 for Android sets a default {{ cssxref("background-image") }} gradient on all buttons (see {{bug(763671)}}). This can be disabled using background-image: none. Clicking and focus Whether clicking on a {{HTMLElement("button")}} causes it to (by default) become focused varies by browser and OS. The results for {{HTMLElement("input")}} of type="button" and type="submit" were the same. Does clicking on a {{HTMLElement("button")}} give it the focus? Desktop Browsers Windows 8.1 OS X 10.9 Firefox 30.0 Yes No (even with a tabindex) Chrome 35 Yes Yes Safari 7.0.5 N/A No (even with a tabindex) Internet Explorer 11 Yes N/A Presto (Opera 12) Yes Yes Does tapping on a {{HTMLElement("button")}} give it the focus? 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 Example <button name="button">Click me</button> {{ EmbedLiveSample('Example', 200, 64) }} Please note that this button has CSS applied. Specifications Specification Status Comment {{SpecName('HTML WHATWG', 'forms.html#the-button-element', '<button>')}} {{Spec2('HTML WHATWG')}} {{SpecName('HTML5 W3C', 'forms.html#the-button-element', '<button>')}} {{Spec2('HTML5 W3C')}} {{SpecName('HTML4.01', 'interact/forms.html#h-17.5', '<button>')}} {{Spec2('HTML4.01')}} Browser compatibility {{CompatibilityTable}} Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari Basic support 1.0 {{CompatVersionUnknown}} {{CompatGeckoDesktop("1.0")}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} formaction attribute 9.0 {{CompatVersionUnknown}} {{CompatGeckoDesktop("2.0")}} 10 {{CompatUnknown}} {{CompatUnknown}} formenctype attribute 9.0 {{CompatVersionUnknown}} {{CompatGeckoDesktop("2.0")}} 10 10.6 {{CompatUnknown}} formmethod attribute 9.0 {{CompatVersionUnknown}} {{CompatGeckoDesktop("2.0")}} 10 {{CompatUnknown}} {{CompatUnknown}} autofocus attribute 5.0 {{CompatVersionUnknown}} {{CompatGeckoDesktop("2.0")}} 10 9.6 5.0 menu value for type attribute {{CompatNo}} {{CompatNo}} {{CompatNo}}[1] {{CompatNo}} {{CompatNo}} {{CompatUnknown}} Feature Android Edge Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatGeckoMobile("1.0")}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} formaction attribute {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatGeckoMobile("2.0")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} formenctype attribute {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatGeckoMobile("2.0")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} formmethod attribute {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatGeckoMobile("2.0")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} menu value for type attribute {{CompatUnknown}} {{CompatNo}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} [1] Gecko does not implement this feature yet. See {{bug("1241353")}}. See also Other elements that are used for creating forms: {{HTMLElement("form")}}, {{HTMLElement("datalist")}}, {{HTMLElement("fieldset")}}, {{HTMLElement("input")}},{{HTMLElement("keygen")}}, {{HTMLElement("label")}}, {{HTMLElement("legend")}}, {{HTMLElement("meter")}}, {{HTMLElement("optgroup")}}, {{HTMLElement("option")}}, {{HTMLElement("output")}}, {{HTMLElement("progress")}}, {{HTMLElement("select")}}, {{HTMLElement("textarea")}}. {{HTMLRef}}
HTML <button> 元素代表一個可點擊的按鈕。
<button>
這元素的屬性包含全域屬性.
off
autocomplete="off"
This Boolean attribute indicates that the user cannot interact with the button. If this attribute is not specified, the button inherits its setting from the containing element, for example {{HTMLElement("fieldset")}}; if there is no containing element with the disabled attribute set, then the button is enabled.
Firefox will, unlike other browsers, by default, persist the dynamic disabled state of a {{HTMLElement("button")}} across page loads. Use the {{htmlattrxref("autocomplete","button")}} attribute to control this feature.
application/x-www-form-urlencoded
multipart/form-data
file
text/plain
If this attribute is specified, it overrides the {{htmlattrxref("enctype","form")}} attribute of the button's form owner.
post
get
If specified, this attribute overrides the {{htmlattrxref("method","form")}} attribute of the button's form owner.
_self
_blank
_parent
_top
submit
reset
button
<button> elements are much easier to style than {{HTMLElement("input")}} elements. You can add inner HTML content (think <em>, <strong> or even <img>), and make use of {{Cssxref(":after")}} and {{Cssxref(":before")}} pseudo-element to achieve complex rendering while {{HTMLElement("input")}} only accepts a text value attribute.
<em>
<strong>
<img>
IE7 has a bug where when submitting a form with <button type="submit" name="myButton" value="foo">Click me</button>, the POST data sent will result in myButton=Click me instead of myButton=foo. IE6 has an even worse bug where submitting a form through a button will submit ALL buttons of the form, with the same bug as IE7. This bug has been fixed in IE8.
<button type="submit" name="myButton" value="foo">Click me</button>
POST
myButton=Click me
myButton=foo
Firefox will add, for accessibility purposes, a small dotted border on a focused button. This border is declared through CSS, in the browser stylesheet, but you can override it if necessary to add your own focused style using button{{cssxref("::-moz-focus-inner")}} { }
button{{cssxref("::-moz-focus-inner")}} { }
Firefox will, unlike other browsers, by default, persist the dynamic disabled state of a {{HTMLElement("button")}} across page loads. Setting the value of the {{htmlattrxref("autocomplete","button")}} attribute to off disables this feature. See {{bug(654072)}}.
Firefox <35 for Android sets a default {{ cssxref("background-image") }} gradient on all buttons (see {{bug(763671)}}). This can be disabled using background-image: none.
background-image: none
Whether clicking on a {{HTMLElement("button")}} causes it to (by default) become focused varies by browser and OS. The results for {{HTMLElement("input")}} of type="button" and type="submit" were the same.
type="button"
type="submit"
tabindex
<button name="button">Click me</button>
{{ EmbedLiveSample('Example', 200, 64) }}
Please note that this button has CSS applied.
{{CompatibilityTable}}
formaction
formenctype
formmethod
autofocus
menu
type
[1] Gecko does not implement this feature yet. See {{bug("1241353")}}.
Other elements that are used for creating forms: {{HTMLElement("form")}}, {{HTMLElement("datalist")}}, {{HTMLElement("fieldset")}}, {{HTMLElement("input")}},{{HTMLElement("keygen")}}, {{HTMLElement("label")}}, {{HTMLElement("legend")}}, {{HTMLElement("meter")}}, {{HTMLElement("optgroup")}}, {{HTMLElement("option")}}, {{HTMLElement("output")}}, {{HTMLElement("progress")}}, {{HTMLElement("select")}}, {{HTMLElement("textarea")}}.