幫助我非常新的網(wǎng)絡(luò)開(kāi)發(fā)使用ASP.NET。為什么在調(diào)試我的代碼時(shí),我的web應(yīng)用程序不像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>
問(wèn)題是當(dāng)我點(diǎn)擊選擇按鈕時(shí),F(xiàn)F不執(zhí)行JavaScript“show color ”,而IE執(zhí)行。
function ShowColor() {
alert("You selected " + SelectColor.value);
BodyContent.style.backgroundColor = SelectColor.value;
}
您的javascript函數(shù)應(yīng)該如下所示:
function ShowColor() {
alert("You selected " + document.getElementById("SelectColor").value);
document.body.style.backgroundColor = document.getElementById("SelectColor").value;
}
您需要使用javascript來(lái)選擇實(shí)際的元素。例如document.gelElementById元素的id),然后更改文檔顏色。這應(yīng)該可以在任何瀏覽器中運(yùn)行。
該函數(shù)現(xiàn)在顯示適當(dāng)?shù)倪x定值,并實(shí)際改變網(wǎng)頁(yè)的背景。
試試這個(gè):
<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>