各ブラウザでgetAttributeを使用してcheckbox/radioを取得するchecked値が異なります

885 ワード

次のように
<!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>

 
IE 6/7:false/trueを順次返す
IE 8:空の文字列/checkedを順番に返す
IE 9/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が選択されているかどうかを判断するには、attributeではなくpropertyを使用します.