的區別" type="hidden"/>

input[type="button"]與


<button>標簽 
瀏覽器支持 
所有主流瀏覽器都支持<button>標簽。 
重要事項:如果在HTML表單中使用button元素,不同的瀏覽器會提交不同的值。IE將提交<button>與<button/>之間的文本,而其他瀏覽器將提交value屬性的內容。請在HTML表單中使用input元素來創建按鈕。 
注意事項 
在使用<button>標簽時很容易想當然的當成<input type="button">使用,這很容易產生以下幾點錯誤用法: 
1、通過$('#customBtn').val()獲取<buttonid="customBtn"value="test">按鈕</button>value的值 
在IE(IE內核)下這樣用到得的是值是“按鈕”,而不是“test”,非IE下得到的是“test”。參加上面標紅的第一句話。 
這一點要和<inputtype="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>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<scripttype="text/javascript"src="jquery-1.4.4.min.js"></script>
<scripttype="text/javascript">
$(function(){
$('#test1').click(function(){
alert($('#customBtn').attr('value'));
});
$('#test2').click(function(){
alert($('#customBtn').val());
});
});
</script>
</head>
<body>
<buttonid="customBtn"value="test">&#x6309;&#x94AE;</button>
<inputtype="button"id="test1"value="getattr"/>
<inputtype="button"id="test2"value="getval"/>
</body>
</html>
2、無意中把<button>標簽放到了<form>標簽中,你會發現點擊這個button變成了提交,相當於<input type="submit"/> 
不要把<button>標簽當成<form>中的input元素。 
驗證這一點可以在測試下面的代碼 
復制代碼
<form action="">
<button>button</button>
<inputtype="submit"value="inputsubmit"/>
<inputtype="button"value="inputbutton"/>
</form>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM