W3Cschool
恭喜您成為首批注冊用戶
獲得88經驗值獎勵
有三種方法可以使用按鈕通過設置三個不同的type屬性。
當您設置要submit
按鈕的type
屬性時,按下按鈕將提交包含按鈕的表單。
這是未應用 type
屬性時的默認行為。
提交按鈕具有以下列出的一些附加屬性。
以下代碼顯示了如何將這些屬性應用于按鈕元素。
<!DOCTYPE HTML> <html> <body> <form> <p> <label for="fave">Fruit: <input autofocus id="fave" name="fave" /></label> </p> <p> <label for="name">Name: <input id="name" name="name" /></label> </p> <button type="submit" formaction="http://example.com/form" formmethod="post">Submit Vote</button> </form> </body> </html>
上面的代碼從表單元素中省略了action和method屬性,并通過button元素上的 formaction
和 formmethod
屬性提供了配置。
如果將某個button
的type
屬性設置為reset
,則按下該按鈕可將表單中的所有輸入元素重置為其初始狀態(tài)。
重置按鈕沒有其他屬性。
以下代碼顯示了向HTML文檔添加重置按鈕。
<!DOCTYPE HTML>
<html>
<body>
<form method="post" action="http://example.com/form">
<p>
<label for="fave">Fruit:
<input autofocus id="fave" name="fave" /></label>
</p>
<p>
<label for="name">Name: <input id="name" name="name" /></label>
</p>
<button type="submit">Submit Vote</button>
<button type="reset">Reset</button>
</form>
</body>
</html>
如果將type
屬性設置為button
,則創(chuàng)建一個普通按鈕。它沒有特殊的意義,不會做任何事情,當你按它。
以下代碼顯示了一個通用按鈕。
<!DOCTYPE HTML>
<html>
<body>
<form method="post" action="http://example.com/form">
<p>
<label for="fave">Fruit: <input autofocus id="fave"
name="fave" /></label>
</p>
<p>
<label for="name">Name: <input id="name" name="name" /></label>
</p>
<button type="button">
Do <strong>NOT</strong> press this button
</button>
</form>
</body>
</html>
您可以對按鈕元素中包含的文本設置樣式。
<html>
<body>
<form action="" method="post">
<button type="submit">Submit</button>
<br /><br />
<button type="reset"><b>Clear this form</b> I want to start again</button>
<br /><br />
<button type="button"><img src="http://www.xlrtb.cn/style/download.png" alt="submit" /></button>
</form>
</body>
</html>
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯系方式:
更多建議: