Questa traduzione è incompleta. Collabora alla traduzione di questo articolo dall’originale in lingua inglese.
Sommario
L'elemento HTML <button>
rappresenta un bottone cliccabile.
- Categoria Contenuto di flusso, contenuto testuale, contenuto interattivo, listed, etichettabile e inviabile associato ai form, contenuto palpabile.
- Contenuto permesso Contenuto testuale.
- Omissione dei tag Nessuna, sia il tag di apertura che quello di chiusura sono obbligatori.
- Elementi genitore permessiQualsiasi elemento che accetti contenuto testuale.
- Interfaccia DOM
HTMLButtonElement
Attributi
Questo elemento include gli attributi globali.
autofocus
HTML5- Questo attributo boleano specifica se questo elemento deve avere il focus appena la pagina si è caricata (a meno che l'utente non lo cambi, per esempio cliccando su un altro elemento). Solo gli elementi associati ai form possono avere questo attributo.
autocomplete
- Questo attributo non è standard, ma supportato solo da Firefox. Come comportamento predefinito, Firefox mantiene lo stato dei bottomi dinamicamente disabilitati quando si ricarica la pagina. Impostare questo attributo a
off
(quindiautocomplete="off"
) disabilita questo comportamento. (Vedere il bug 654072). disabled
- Questo attributo boleano indica che l'utente non può interagire con il pulsante. Se questo attributo non è specificato, il bottone eredita questa impostazione dall'elemento che lo contiene, per esempio un
<fieldset>
. -
A differenza degli altri browser, Firefox mantiene lo stato dei bottono dinamicamente disabilitati attraverso i caricamenti della pagina. Per prevenire questo comportamento, usare l'attributo
autocomplete
. form
HTML5- Questo attributo specifica a quale form è associato questo bottone. Il valore di questo attributo deve essere l'id di un elemento
<form>
nello stesso documento. Se non è specificato, il bottone è associato al primo elemento<form>
di livello superiore. Questo attributo consente di posizionare gli elementi<button>
ovunque nella pagina, non necessariamente all'interno dei relativi<form>
. formaction
HTML5- The URI of a program that processes the information submitted by the button. If specified, it overrides the
action
attribute of the button's form owner. formenctype
HTML5- 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<input>
element with thetype
attribute set tofile
.text/plain
If this attribute is specified, it overrides the
enctype
attribute of the button's form owner. formmethod
HTML5- 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
method
attribute of the button's form owner. formnovalidate
HTML5- 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
novalidate
attribute of the button's form owner. formtarget
HTML5- 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
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 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
.
name
- The name of the button, which is submitted with the form data.
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.
value
- The initial value of the button.
Example
<button name="button">Click me</button>
Please note that this button has CSS applied.
Specifications
Specification | Status | Comment |
---|---|---|
WHATWG HTML Living Standard The definition of '<button>' in that specification. |
Living Standard | |
HTML5 The definition of '<button>' in that specification. |
Recommendation | |
HTML 4.01 Specification The definition of '<button>' in that specification. |
Recommendation |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 1.0 | 1.0 (1.7 or earlier) | (Yes) | (Yes) | (Yes) |
formaction attribute |
9.0 | 4.0 (2.0) | 10 | ? | ? |
formenctype attribute |
9.0 | 4.0 (2.0) | 10 | 10.6 | ? |
formmethod attribute |
9.0 | 4.0 (2.0) | 10 | ? | ? |
autofocus attribute |
5.0 | 4.0 (2.0) | 10 | 9.6 | 5.0 |
Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | (Yes) | 1.0 (1.0) | (Yes) | (Yes) | (Yes) |
formaction attribute |
? | 4.0 (2.0) | ? | ? | ? |
formenctype attribute |
? | 4.0 (2.0) | ? | ? | ? |
formmethod attribute |
? | 4.0 (2.0) | ? | ? | ? |
Clicking and focus
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"
were the same.
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 |
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 |
Notes
<button>
elements are much easier to style than <input>
elements. You can add inner HTML content (think <em>
, <strong>
or even <img>
), and make use of :after
and :before
pseudo-element to achieve complex rendering while <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
::-moz-focus-inner
{ }
Firefox will, unlike other browsers, by default, persist the dynamic disabled state of a <button>
across page loads. Setting the value of the autocomplete
attribute to off
disables this feature. See bug 654072.
Firefox <35 for Android sets a default background-image
gradient on all buttons (see bug 763671). This can be disabled using background-image: none
.
See also
Other elements that are used for creating forms: <form>
, <datalist>
, <fieldset>
, <input>
,<keygen>
, <label>
, <legend>
, <meter>
, <optgroup>
, <option>
, <output>
, <progress>
, <select>
, <textarea>
.