2012-05-13 71 views
0

幫助我對使用ASP.NET開發Web很有新意。爲什麼我的web應用程序像IE所需的輸出如下調試我的代碼什麼時候不給:Firefox中的Javascript問題,但不在IE中調試問題VIsual Studio 2010

<!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的「ShowColor」當我點擊選擇按鈕,但IE瀏覽器。

enter image description here

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

你問的問題是不明確的。首先你提到Firefox是問題,然後你提到,「問題是,當我點擊Select按鈕時,IE執行javascript」ShowColor「,問題是句子中的關鍵詞。 – jacqijvv

+0

感謝您的評論。我真的很抱歉。我試圖表達的是,Firefox不能像IE一樣正確地輸出它,因爲firefox不會執行jscript – Jed

回答

3

你的JavaScript函數應該如下:

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

您需要選擇使用javascript的實際元素。例如document.gelElementById(「元素的id」),然後更改文檔顏色。這應該適用於任何瀏覽器。

現在的函數示出了適當的選擇的值和實際改變了網頁的背景。如果這有助於你,請標記爲答案。

+0

非常感謝@jacqijw。如我錯了請糾正我。我只是意識到,asp.net代碼(我應該說VS網絡應用代碼)只適用於IE。謝謝你的協助。我現在可以繼續教程。 – Jed

+0

@Jed - 這是你不應該將IE看作是應該如何工作的參考的另一個原因。 – Rob

+0

@jacqijw:你可以幫我這個: [鏈接](http://stackoverflow.com/questions/10629423/asp-net-mvc3-site-css/10629447#comment13777779_10629447) – Jed

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>