zoukankan      html  css  js  c++  java
  • 各浏览器中使用getAttribute获取checkbox/radio的checked值不同

    如下

    <!DOCTYPE HTML>
    <head>
    	<meta charset="utf-8" />
    </head>
    <body>
    <input type="checkbox" />
    <input type="checkbox" checked/>
    
    <script>
    	var checkboxs = document.getElementsByTagName('input');
    	alert(checkboxs[0].getAttribute('checked'));
    	alert(checkboxs[1].getAttribute('checked'));
    </script>
    </body>
    </HTML>

    IE6/7 :依次返回 false/true

    IE8 :依次返回 空字符串/checked

    IE9/10/Firefox/Safari/Chrome/Opera :依次返回 null/空字符串

     

    input[type=radio]的情况同上,类似的布尔属性还有:autofocus|autoplay|async|checked|controls|defer|disabled|hidden|loop|multiple|open|readonly|required|scoped|selected

    注意:要判断checkbox/radio有没有选定,使用property而不要使用attribute。

  • 相关阅读:
    H
    饭卡(0 1背包)
    J
    H
    E
    3991: [SDOI2015]寻宝游戏
    CF 1051 F. The Shortest Statement
    CF 643 E. Bear and Destroying Subtrees
    4567: [Scoi2016]背单词
    4946: [Noi2017]蔬菜
  • 原文地址:https://www.cnblogs.com/snandy/p/2162088.html
Copyright © 2011-2022 走看看