<button> 标签订义一个按钮。javascript
在 button 元素内部,您能够放置内容,好比文本或图像。html
<button> 控件 与 <input type="button"> 相比,提供了更为强大的功能和更丰富的内容。<button> 与 </button> 标签之间的全部内容都是按钮的内容,其中包括任何可接受的正文内容,好比文本或多媒体内容。java
惟一禁止使用的元素是图像映射,由于它对鼠标和键盘敏感的动做会干扰表单按钮的行为。jquery
请始终为按钮规定 type 属性。Internet Explorer 的默认类型是 "button",而其余浏览器中(包括 W3C 规范)的默认值是 "submit"。浏览器
浏览器支持测试
全部主流浏览器都支持 <button> 标签。ui
重要事项:若是在 HTML 表单中使用 button 元素,不一样的浏览器会提交不一样的值。Internet Explorer 将提交 <button> 与 <button/> 之间的文本,而其余浏览器将提交 value 属性的内容。请在 HTML 表单中使用 input 元素来建立按钮。spa
注意事项code
在使用<button>标签时很容易想固然的当成 <input type="button">使用,这很容易产生如下几点错误用法:orm
一、经过$('#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">按钮</button> <input type="button" id="test1" value="get attr"/> <input type="button" id="test2" value="get val"/> </body> </html>
二、无心中把<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>