87,990
社区成员
发帖
与我相关
我的任务
分享<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>hasAttribute BUG in all browser?</title>
<script type="text/javascript" language="javascript">
// <![CDATA[
window.onload = function() {
var br = "\u000d\u000a",
str = navigator.userAgent+br,
abc = document.getElementById("abc");
abc.checked = true;
try {
str += "typeof "+(typeof abc["checked"] !== "undefined")+br;
str += "getAttribute "+(abc.getAttribute("checked") !== null)+br;
str += "hasAttribute "+(abc.hasAttribute("checked"))+br;
str += "hasOwnProperty "+(abc.hasOwnProperty("checked"));
} catch(e) {}
document.body.appendChild(document.createElement("PRE").appendChild(document.createTextNode(str)).parentNode);
};
// ]]>
</script>
</head>
<body>
<input type="radio" id="abc" />
</body>
</html><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" language="javascript">
// <![CDATA[
window.onload = function() {
var abc = document.getElementById("abc");
abc.checked = false;
alert(hasAttr(abc, "checked"));
};
function hasAttr(element, name) {
//return element.attributes[name] !== undefined; // FF中既然返回false,IE8照样返回true
return typeof element[name] !== "undefined"; // 2者都返回true
}
// ]]>
</script>
</head>
<body>
<input type="checkbox" name="abc[]" id="abc" />
</body>
</html>function hasAttr(element, name) {
return element.attributes[name] !== undefined;
}