[英]why doesn't this script work and…?
html代碼:
<!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;
}
當我從組合框中選擇一個選項時,為什么不進入新頁面,即值? 還有document.getElementById("newLocation");
此語句(函數jumpPage的第一條語句)返回?
你可以試試
var newPage = newLoc.options[newLoc.selectedIndex].value;
該聲明
var newLoc = document.getElementById("newLocation");
只是找到DOM(HTML)元素<... id="newLocation" ...>
,即您的<select id="newLocation">
。
document.getElementById("newLocation")
將返回SELECT對象(即對您的下拉菜單的引用)。
關於JS,它有一個錯誤。 您應該將newLoc.getSelectedIndex
更改為newLoc.selectedIndex
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.