innerhtml firefox无法使用Doc类型

时间:2012-03-27 12:45:44

标签: javascript html innerhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"> 
<html>
<head>
<script type="text/javascript">
function selectSomething(){
    var obj = document.all.select1;
    var selectValue = obj.options[obj.selectedIndex].value;

    if(selectValue == "1"){
        document.getElementById("text").innerHTML ="one";
    }
    if(selectValue == "2"){
        document.getElementById("text").innerHTML ="two";
    }
}
</script>
</head>
<body onload="selectSomething()">

<select id="select1" onchange="selectSomething()">
<option value="1">One</option>
<option value="2">Two</option>
</select>

<div id="text"></div>

</body>
</html>

此代码适用于Chrome 17.0.963.83,IE 8.0.6001.18702,但在Firefox 11.0中不起作用。 innerhtml永远不会在Firefox中显示,除非我删除了我不能做的DOCTYPE,因为这个网页将成为更大项目的一部分。这是Firefox的错误吗?如何在Firefox中使用它?

4 个答案:

答案 0 :(得分:3)

查看Firefox错误控制台(对Web开发人员非常有用)。在Firefox中,document.all未定义。

相反,您应该使用document.getElementById("select1")

答案 1 :(得分:2)

Firefox不支持document.all,而不是

var obj = document.all.select1;

使用

var obj = document.getElementById("select1");

(你也错过了一个xmlns)

答案 2 :(得分:1)

这项工作

 var obj = document.getElementById("select1");

答案 3 :(得分:1)

尝试使用document.getElementById代替document.allall已被弃用,因此当您使用XHTML作为文档类型时,我猜测它会死亡。