Firefox中的Javascript问题但不是IE调试问题VIsual Studio 2010

时间:2012-05-13 09:25:44

标签: javascript asp.net css html5

帮助我使用ASP.NET进行Web开发非常新。为什么我的网络应用程序在调试下面的代码时没有像IE那样提供所需的输出:

<!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></title>
<style type="text/css">
    h1{color:Blue}
    h2{color:Red}

</style>
<script type="text/javascript">
    function ShowColor() {
        alert("You selected " + SelectColor.value);
        BodyContent.style.backgroundColor = SelectColor.value;
    }
</script>
</head>
<body>
<div id="BodyContent">
    <h1>HelloWorld</h1>
    <h2>Welcome</h2>
    <p>
    This is my first Web Page</p>
    <hr />
    Please select color:
    <select id="SelectColor">
        <option value="white">white</option>
        <option value="yellow">yellow</option>
        <option value="silver">silver</option>
    </select>
    <input id="ButtonColor" type="button" value="Select" onclick="ShowColor()" />
</div>

</body>
</html>

问题是FF不执行javascript&#34; ShowColor&#34;当我点击选择按钮但IE确实如此。

enter image description here

    function ShowColor() {
        alert("You selected " + SelectColor.value);
        BodyContent.style.backgroundColor = SelectColor.value;
    }

2 个答案:

答案 0 :(得分:3)

您的javascript函数应如下所示:

function ShowColor() {
    alert("You selected " + document.getElementById("SelectColor").value);
    document.body.style.backgroundColor = document.getElementById("SelectColor").value;
}

您需要使用javascript选择实际元素。例如document.gelElementById(“id of element”),然后更改文档颜色。这应该可以在任何浏览器中使用。

该功能现在显示适当的选定值,并实际更改网页的背景。如果这有助于你,请标记为答案。

答案 1 :(得分:1)

试试这个:

<script type="text/javascript">
var selected;
function alertselected(selectobj) {
    selected = selectobj.selectedIndex;
}

function ShowColor() {
    alert("You selected " + selected);
    elm = document.getElementById("sample");
    document.getElementById("BodyContent").style.backgroundColor = elm.options[elm.selectedIndex].value;
}

HTML:

<div id="BodyContent"><select id="sample" onChange="alertselected(this)">option>white</option><option>yellow</option><option>silver</option>

<input id="ButtonColor" type="button" value="Select" onclick="ShowColor()" /></div>