是<button type="button>
與簡單的<button>
有什麼不同?空白或缺少type
屬性? MDN和HTML5規範說type=button
是用於觸發自定義JavaScript的按鈕,但是這不是<button>
默認情況下的功能嗎?<button type =「button」>有什麼意義?
4
A
回答
12
是的,有一個原因 - 但(通常)只有當你在<form>
元素。
如果你在一個表單元素中包含一個按鈕,而沒有指定它只是一個普通的按鈕,它默認爲一個提交按鈕。
<form>
<button>I will submit the form when clicked!</button>
</form>
VS
<form>
<button type='button'>I won't!</button>
</form>
第一個被假定爲type=submit
因爲尚未指定一個type
屬性。
如果你是在<form>
元素不,按鈕不會有什麼提交,所以它並不重要一樣多。 :)
雖然語義通常在應用程序的生命週期中的某個時刻變得很重要,但是,養成指定type
的習慣是個好主意。
唯一的其他原因,可能是相關的是,如果有一個造型規則,指定[type=button]
什麼的。不過這不推薦。
1
還有<button type="submit">
點擊時提交表單。
所以,如果你想使用AJAX執行POST,那麼最好使用<button type="button">
,這樣當點擊按鈕時它不會提交表單。
1
默認按鈕類型實際上是submit
。這說明應該沒有帖子選項,但只是一個可點擊的對象。
2
<button>
默認爲type = "submit"
這意味着如果它在一個表單元素中,它會嘗試提交表單。您可以將您的點擊事件綁定到它並執行一些操作。
<button type="button">
意味着它是一個正常的按鈕,你必須點擊事件綁定到它做一些動作。
4
按鈕的默認類型是「submit」。就我所見,MDN不會談論它,但它可在html5 specification中獲得。
缺失值的默認值是Submit Button狀態。
因此,將類型設置爲「按鈕」會禁用提交表單的默認行爲,因此您不需要使用preventDefault來在JavaScript中處理它。
相關問題
- 1. <button>有什麼區別Click Me!</button>和<button type =「button」>點擊我!</button>?
- 2. <input type =「button」/>和<button>之間有什麼區別?
- 3. 是否需要定義<button> TYPE?
- 4. IE7 <button>和<input type = button/submit>怪異的造型?
- 5. <button>與<INPUT TYPE =「圖像」>
- 6. <a><button> ...</button></a>和<button><a> ...</a></button>有什麼區別?
- 7. 爲什麼`<Button Command = {x:Static ...} ..>`但是`<Button Command = {Binding ...} ..>`不?
- 8. jQuery選擇<button></button>
- 9. <input type ='button'/>和<input type ='submit'/>之間的區別
- 10. IE 7不喜歡jquery('<button/>').attr('type','button')
- 11. <button type =「button」>在提交時用作表單值
- 12. <button type =「submit」>兼容性?
- 13. <button type =「submit」>不工作在FireFox
- 14. <input type =「submit」> button和javascripts .submit()
- 15. 使用<button type =「submit」>保存</button>並不一致觸發
- 16. 爲什麼<input type =「button」>提交表單?
- 17. <button>與<fieldset>
- 18. <a>和<button>
- 19. $ _GET值<button>?
- 20. 使用Selenium Webdriver和ruby使用<button type =「button」>上傳文件>
- 21. 檢測點擊<button></button>通過PHP
- 22. 按<button> CSS
- 23. 在今天的現代瀏覽器中,使用輸入[type =「button」]比使用<button>有什麼優勢?
- 24. 製作一個<i>標記一個<input type ='button'>
- 25. 爲什麼<button>在<label>上觸發兩次?
- 26. rails button_to type =「button」
- 27. <button>事件不
- 28. 點擊<button>標籤
- 29. 控制<param>值與<button>
- 30. 使用<button>代替<a>