<button>和<input type="button"> 的區別

來源:互聯網
上載者:User

標籤:

定義和用法

<button> 標籤定義一個按鈕。

在 button 元素內部,您可以放置內容,比如文本或映像。這是該元素與使用 input 元素建立的按鈕之間的不同之處。

<button> 控制項 與 <input type="button"> 相比,提供了更為強大的功能和更豐富的內容。<button> 與 </button> 標籤之間的所有內容都是按鈕的內容,其中包括任何可接受的本文內容,比如文本或多媒體內容。例如,我們可以在按鈕中包括一個映像和相關的文本,用它們在按鈕中建立一個迷人的標記映像。

唯一禁止使用的元素是影像地圖,因為它對滑鼠和鍵盤敏感的動作會干擾表單按鈕的行為。

請始終為按鈕規定 type 屬性。Internet Explorer 的預設類型是 "button",而其他瀏覽器中(包括 W3C 規範)的預設值是 "submit"。

瀏覽器支援

所有主流瀏覽器都支援 <button> 標籤。

重要事項:如果在 HTML 表單中使用 button 元素,不同的瀏覽器會提交不同的值。Internet Explorer 將提交 <button> 與 <button/> 之間的文本,而其他瀏覽器將提交 value 屬性的內容。請在 HTML 表單中使用 input 元素來建立按鈕。

注意事項

在使用<button>標籤時很容易想當然的當成 <input type="button">使用,這很容易產生以下幾點錯誤用法:

 1、通過$(‘#customBtn‘).val()擷取<button id="customBtn" value="test">按鈕</button> value的值

     在IE(IE核心)下這樣用到得的是值是“按鈕”,而不是“test”,非IE下得到的是“test”。 參加上面標紅的第一句話。

  這一點要和<input type="button">區分開。                         

     通過這兩種方式$(‘#customBtn‘).val(),$(‘#customBtn‘).attr(‘value‘)在不同瀏覽器的獲得值,如下: 

 

Browser/Value

$(‘#customBtn‘).val()

$(‘#customBtn‘).attr(‘value‘)

Firefox13.0

test

test

Chrome15.0

test

test

Opera11.61

test

test

Safari5.1.4

test

test

IE9.0

按鈕

按鈕

驗證這一點可以在測試下面的代碼 

<html> <head>  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  <script type="text/javascript" src="jquery-1.4.4.min.js"></script>  <script type="text/javascript">    $(function() {        $(‘#test1‘).click(function() {            alert($(‘#customBtn‘).attr(‘value‘));            });        $(‘#test2‘).click(function() {            alert($(‘#customBtn‘).val());            });    });  </script> </head> <body>    <button id="customBtn" value="test">&#x6309;&#x94AE;</button>     <input type="button" id="test1" value="get attr"/>    <input type="button" id="test2" value="get val"/> </body></html>

2、無意中把<button>標籤放到了<form>標籤中,你會發現點擊這個button變成了提交,相當於<input type="submit"/>

    這一點參見上面第二句標紅的話就明白什麼意思了。

    不要把<button>標籤當成<form>中的input元素。

    驗證這一點可以在測試下面的代碼 

<html><body>    <form action="">        <button> button </button>        <input type="submit" value="input submit"/>        <input type="button" value="input button"/>    </form></body></html>

轉自:http://www.cnblogs.com/purediy/archive/2012/06/10/2544184.html

<button>和<input type="button"> 的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.