<!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>
<title>form表单元素判断在ie6中的bug</title>
</head>
<body>
<form id="ff"></form>
<script type="text/javascript">
<!--
function testfun() {
var f = document.getElementById("ff");
if (f["detail"]) {
f["detail"].value = "111";
} else {
var o = document.createElement("input");
o.name = "detail";
f.appendChild(o);
}
}
testfun();
testfun();
// -->
</script>
</body>
</html>
第一次调用js会产生form的一个detail表单,第二次调用判断是否已经存在了。
在分firefox正常,在ie6里不正常。
我们再看一个代码:
<!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>
<title>form表单元素判断在ie6中的bug</title>
</head>
<body>
<form id="ff">
<input type="text" name="detail" />
</form>
<script type="text/javascript">
<!--
function testfun() {
var f = document.getElementById("ff");
if (f["detail"]) {
f["detail"].value = "111";
} else {
var o = document.createElement("input");
o.name = "detail";
f.appendChild(o);
}
}
//testfun();
testfun();
// -->
</script>
</body>
</html>
这个写法在ie6和firefox都显示正常。
两者比较一下,发现ie6就是这么垃圾,对于js动态写入的元素根本无法判断。