解析<button>和<input type="button"> 的区别

转自:http://zljpp.iteye.com/blog/1561329


一、定义和用法

<button> 标签定义的是一个按钮。

在 button 元素内部,可以放置文本或图像。这是<button>与使用 input 元素创建的按钮的不同之处。

二者相比较, <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">按钮</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>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是对这段代码的优化: 1. 在`<meta>`标签中添加`viewport`,以适应不同设备的屏幕大小。同时,将`<meta>`标签移动到`<title>`标签前面,符合 HTML 5 标准。 ```html <meta name="viewport" content="width=device-width, initial-scale=1.0"> ``` 2. 将样式表放到`<head>`标签内,并将`type`属性设置为`text/css`,这样浏览器可以正确解析样式表。 ```html <style type="text/css"> /* 样式表代码 */ </style> ``` 3. 将`<script>`标签放到`<body>`标签底部,这样可以让页面更快地加载。 4. 将`<table>`标签的`border`、`cellspacing`和`cellpadding`属性移除,这些属性可以使用 CSS 设置。 5. 将重复的`id`属性`button`修改为`button-calculate`和`button-reset`。 6. 将`<input>`标签的`type`属性设置为`number`,这样用户只能输入数字。 7. 将`onclick`属性修改为`addEventListener()`,这样可以避免全局作用域污染。 8. 将`show()`函数改为`calculate()`,这样更符合函数的语义。 ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>计算∑N!</title> <style type="text/css"> table { background-color: #f2f2f2; width: 600px; height: 300px; margin: 20px auto; color: black; border: 20px ridge #9999cc; border-collapse: collapse; } td { font-size: 20px; font-weight: bold; text-align: center; padding: 10px; border: 1px solid #9999cc; } #button-calculate, #button-reset { width: 180px; height: 60px; } </style> </head> <body> <table> <tr> <td colspan="4">计算∑N!</td> </tr> <tr> <td colspan="2">输入整数N的值:</td> <td colspan="2"><input type="number" id="n_text" value="" /></td> </tr> <tr> <td colspan="2">∑N!=</td> <td colspan="2"><input type="text" id="sum_text" value="" /></td> </tr> <tr> <td colspan="2"><input type="button" id="button-calculate" value="计算∑N!" /></td> <td colspan="2"><input type="reset" id="button-reset" value="清空" /></td> </tr> </table> <script src="sum_factorial.js"></script> <script> const buttonCalculate = document.getElementById("button-calculate"); buttonCalculate.addEventListener("click", calculate); function calculate() { const n = parseInt(document.getElementById("n_text").value); const sum = sumFactorial(n); document.getElementById("sum_text").value = sum; } </script> </body> </html> ``` 注意:这里只是对 HTML 和 CSS 的优化,对于`sum_factorial.js`文件中的代码没有进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值