傳統
這裡要做的是,省略Go There按鈕,選擇菜單項後,直接跳轉。
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>轉移菜單</title>
<script type="text/javascript" src="轉移菜單.js"></script>
</head>
<body>
<form>
<select id="newLocation">
<option selected="selected" value="">Select a topic</option>
<option value="topic1.html">topic1</option>
<option value="topic2.html">topic2</option>
<option value="topic3.html">topic3</option>
<option value="topic4.html">topic4</option>
<option value="topic5.html">topic5</option>
</select>
<!--當浏覽器不支持javascript或者禁用腳本運行時被調用-->
<noscript>
<input type="submit" value="Go There!"/>
</noscript>
</form>
</body>
</html>
Javascript腳本
復制代碼 代碼如下:
window.onload=initForm;
//防止頁面緩存,無法觸發onload
window.onunload=function(){}
function initForm(){
document.getElementById("newLocation").selectIndex=0;
document.getElementById("newLocation").onchange=jumpPage;
}
function jumpPage(){
var newLoc=document.getElementById("newLocation");
var newPage=newLoc.options[newLoc.selectedIndex].value;
if (newPage!=""){
window.location=newPage;
}
}