开发者

why doesn't this script work and...?

html code :

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="script.js">
</script>
</head>

<body bgcolor="#FFFFCC">
<center>
<form>
 <select id="newLocation">
  <option value="1.jpg">index</option>
   <option value="script.js">posts</option>
   <option value="2.jpg" selected="selected">blog</option>
 </select>
</form>

javascript :

window.onload = startInit;

function startInit() {
document.getElementById("newLocation").selectedIndex = 0;
document.getElementById("newLocation").onchange = jumpPage;
}

function jumpPage() {
var newLoc = document.getElementById("newLocation");// what does this statement return ?
var newPage = newLoc.options[newLoc.getSelectedIndex].value;
if(newPage != "")
   window.location = newPage;
}

Why don't get to to new page i.e the value when i select an option from the com开发者_JAVA百科bo box ? Also what does document.getElementById("newLocation"); this statement (the first statement of the function jumpPage) return ?


You can try

var newPage = newLoc.options[newLoc.selectedIndex].value;

The statement

var newLoc = document.getElementById("newLocation"); 

just finds the DOM (HTML) element <... id="newLocation" ...>, i.e. your <select id="newLocation"> .


document.getElementById("newLocation") will return you the SELECT object (i.e., the reference to your drop-down).

Regarding the JS, it has an error. You should change newLoc.getSelectedIndex to newLoc.selectedIndex.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜