Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript innerhtml firefox不使用文档类型 函数selectSomething(){ var obj=document.all.select1; var selectValue=obj.options[obj.selectedIndex].value; 如果(选择值=“1”){ document.getElementById(“text”).innerHTML=“one”; } 如果(选择值=“2”){ document.getElementById(“text”).innerHTML=“两个”; } } 一 二_Javascript_Html_Innerhtml - Fatal编程技术网

Javascript innerhtml firefox不使用文档类型 函数selectSomething(){ var obj=document.all.select1; var selectValue=obj.options[obj.selectedIndex].value; 如果(选择值=“1”){ document.getElementById(“text”).innerHTML=“one”; } 如果(选择值=“2”){ document.getElementById(“text”).innerHTML=“两个”; } } 一 二

Javascript innerhtml firefox不使用文档类型 函数selectSomething(){ var obj=document.all.select1; var selectValue=obj.options[obj.selectedIndex].value; 如果(选择值=“1”){ document.getElementById(“text”).innerHTML=“one”; } 如果(选择值=“2”){ document.getElementById(“text”).innerHTML=“两个”; } } 一 二,javascript,html,innerhtml,Javascript,Html,Innerhtml,这段代码适用于Chrome17.0.963.83,即8.0.6001.18702,但不适用于Firefox11.0。innerhtml永远不会在Firefox中显示,除非我删除DOCTYPE,我不能这样做,因为这个网页将是一个更大项目的一部分。这是Firefox的bug吗?如何在Firefox中实现这一点?看看Firefox错误控制台(对web开发人员非常有用)。在Firefox中,document.all是未定义的 相反,您应该使用document.getElementById(“select

这段代码适用于Chrome17.0.963.83,即8.0.6001.18702,但不适用于Firefox11.0。innerhtml永远不会在Firefox中显示,除非我删除DOCTYPE,我不能这样做,因为这个网页将是一个更大项目的一部分。这是Firefox的bug吗?如何在Firefox中实现这一点?

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


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

Firefox不支持
document.all
因此而不是

<!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>
使用

(您还缺少一个xmlns)

这项工作

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

尝试使用
document.getElementById
而不是
document.all
all
已被弃用,因此我猜当您使用XHTML作为您的doctype时,这一点正在消失。

要弃用它,首先必须是标准的。这只是一个IE4ism。@Quentin不,它只是被用来反对;它不必首先成为一个标准。即使在为IE编写页面时,也不鼓励使用
document.all
,因此不推荐使用。另外,请注意,OP声明此代码在没有doctype(怪癖模式)的FF上工作。为什么有框架集doctype?!刚加载它,就看到了“document.all is undefined var obj=document.all.select1;”错误。按你说的做了这个开关,它成功了。谢谢大家!@bitnuk3r:很高兴听到这个消息。我发现在Firefox(比IE更符合标准)中开始开发,然后进行任何必要的调整以使其在其他浏览器中工作是最好的做法。对网络开发者来说,一个非常有用的Firefox插件是。是的,我应该在发布之前检查一下。我猜当我看到一个错误时,我的直觉是“StackO it”,因为这里的社区非常棒,解决方案发布得非常快。再次感谢。顺便说一句,卡尔文和霍布斯是一部很棒的连环漫画!
var obj = document.getElementById("select1");
 var obj = document.getElementById("select1");