|
| 1 | +# <button> 元素 |
| 2 | + |
| 3 | +`<button>`元素继承了`HTMLButtonElement`接口。它有以下的实例属性。 |
| 4 | + |
| 5 | +**(1)HTMLButtonElement.accessKey** |
| 6 | + |
| 7 | +`HTMLButtonElement.accessKey`属性返回一个字符串,表示键盘上对应的键,通过`Alt + 这个键`可以让按钮获得焦点。该属性可读写。 |
| 8 | + |
| 9 | +**(2)HTMLButtonElement.autofocus** |
| 10 | + |
| 11 | +`HTMLButtonElement.autofocus`属性是一个布尔值,表示页面加载过程中,按钮是否会自动获得焦点。该属性可读写。 |
| 12 | + |
| 13 | +**(3)HTMLButtonElement.disabled** |
| 14 | + |
| 15 | +`HTMLButtonElement.disabled`属性是一个布尔值,表示该按钮是否禁止点击。该属性可读写。 |
| 16 | + |
| 17 | +**(4)HTMLButtonElement.form** |
| 18 | + |
| 19 | +`HTMLButtonElement.form`属性是一个表单元素,返回该按钮所在的表单。该属性只读。如果按钮不属于任何表单,该属性返回`null`。 |
| 20 | + |
| 21 | +**(5)HTMLButtonElement.formAction** |
| 22 | + |
| 23 | +`HTMLButtonElement.formAction`返回一个字符串,表示表单提交的 URL。该属性可读写,一旦设置了值,点击按钮就会提交到该属性指定的 URL,而不是`<form>`元素指定的 URL。 |
| 24 | + |
| 25 | +**(6)HTMLButtonElement.formEnctype** |
| 26 | + |
| 27 | +`HTMLButtonElement.formEnctype`属性是一个字符串,表示数据提交到服务器的编码类型。该属性可读写,一旦设置了值,点击按钮会按照该属性指定的编码方式,而不是`<form>`元素指定的编码方式。 |
| 28 | + |
| 29 | +该属性可以取以下的值。 |
| 30 | + |
| 31 | +- `application/x-www-form-urlencoded`(默认值) |
| 32 | +- `multipart/form-data`(上传文件的编码方式) |
| 33 | +- `text/plain` |
| 34 | + |
| 35 | +**(7)HTMLButtonElement.formMethod** |
| 36 | + |
| 37 | +`HTMLButtonElement.formMethod`属性是一个字符串,表示浏览器提交表单的 HTTP 方法。该属性可读写,一旦设置了值,点击后就会采用该属性指定的 HTTP 方法,而不是`<form>`元素指定的编码方法。 |
| 38 | + |
| 39 | +**(8)HTMLButtonElement.formNoValidate** |
| 40 | + |
| 41 | +`HTMLButtonElement.formNoValidate`属性是一个布尔值,表示点击按钮提交表单时,是否要跳过表单校验的步骤。该属性可读写,一旦设置会覆盖`<form>`元素的`novalidate`属性。 |
| 42 | + |
| 43 | +**(9)HTMLButtonElement.formTarget** |
| 44 | + |
| 45 | +`HTMLButtonElement.formTarget`属性是一个字符串,指定了提交了表单以后,哪个窗口展示服务器返回的内容。该属性可读写,一旦设置会覆盖`<form>`元素的`target`属性。 |
| 46 | + |
| 47 | +**(10)HTMLButtonElement.labels** |
| 48 | + |
| 49 | +`HTMLButtonElement.labels`返回`NodeList`实例,表示那些绑定按钮的`<label>`元素。该属性只读。 |
| 50 | + |
| 51 | +```javascript |
| 52 | +/* HTML 代码如下 |
| 53 | + <label id="label1" for="test">Label 1</label> |
| 54 | + <button id="test">Button</button> |
| 55 | + <label id="label2" for="test">Label 2</label> |
| 56 | +*/ |
| 57 | + |
| 58 | +const button = document.getElementById('test'); |
| 59 | + |
| 60 | +for(var i = 0; i < button.labels.length; i++) { |
| 61 | + console.log(button.labels[i].textContent); |
| 62 | +} |
| 63 | +// "Label 1" |
| 64 | +// "Label 2" |
| 65 | +``` |
| 66 | + |
| 67 | +上面代码中,两个`<label>`元素绑定`<button>`元素。`button.labels`返回这两个`<label>`元素。 |
| 68 | + |
| 69 | +**(11)HTMLButtonElement.name** |
| 70 | + |
| 71 | +`HTMLButtonElement.name`属性是一个字符串,表示按钮元素的`name`属性。如果没有设置`name`属性,则返回空字符串。该属性可读写。 |
| 72 | + |
| 73 | +**(12)HTMLButtonElement.tabIndex** |
| 74 | + |
| 75 | +`HTMLButtonElement.tabIndex`是一个整数,代表按钮元素的 Tab 键顺序。该属性可读写。 |
| 76 | + |
| 77 | +**(13)HTMLButtonElement.type** |
| 78 | + |
| 79 | +`HTMLButtonElement.type`属性是一个字符串,表示按钮的行为。该属性可读写,可能取以下的值。 |
| 80 | + |
| 81 | +- `submit`:默认值,表示提交表单。 |
| 82 | +- `reset`:重置表单。 |
| 83 | +- `button`:没有任何默认行为。 |
| 84 | + |
| 85 | +**(14)HTMLButtonElement.validationMessage** |
| 86 | + |
| 87 | +`HTMLButtonElement.validationMessage`属性是一个字符串,表示没有通过校验时显示的提示信息。该属性只读。 |
| 88 | + |
| 89 | +**(15)HTMLButtonElement.validity** |
| 90 | + |
| 91 | +`HTMLButtonElement.validity`属性返回该按钮的校验状态(`ValidityState`)。该属性只读。 |
| 92 | + |
| 93 | +**(16)HTMLButtonElement.value** |
| 94 | + |
| 95 | +`HTMLButtonElement.value`属性返回该按钮绑定的值。该属性可读写。 |
| 96 | + |
| 97 | +**(17)HTMLButtonElement.willValidate** |
| 98 | + |
| 99 | +`HTMLButtonElement.willValidate`属性是一个布尔值,表示该按钮提交表单时是否将被校验,默认为`false`。该属性只读。 |
| 100 | + |
0 commit comments