<!-- 1. 通用跳轉函數 -->
<script>
function jumpMenu(target, selectObj, restore) {
// target: 目標窗口 ('parent', '_top', '_blank', 'iframeName')
// selectObj: select 元素 (this)
// restore: 是否重置選中索引為 0
var url = selectObj.options[selectObj.selectedIndex].value;
if (url) {
if (target === 'parent') {
window.parent.location.href = url;
} else if (target === '_top') {
window.top.location.href = url;
} else if (target === '_blank') {
window.open(url);
} else {
// 視為 iframe name 或當前窗口
var frame = window.frames[target] || window;
frame.location.href = url;
}
}
if (restore) {
selectObj.selectedIndex = 0;
}
}
</script>
<!-- 2. HTML 使用示例 -->
<p class="links">友情鏈接:</p>
<select onchange="jumpMenu('parent', this, 0)">
<option value="">請選擇網站</option>
<option value="https://www.fudan.org.cn/">復旦大學校友網</option>
<option value="https://www.pku.org.cn/">北京大學校友網</option>
<option value="https://www.tsinghua.org.cn/alumni/index.do">清華大學校友網</option>
<option value="https://www.example.com/" target="_blank">新窗口打開示例</option>
</select>
function jumpMenuEnhanced(target, selectObj, restore) {
var opt = selectObj.options[selectObj.selectedIndex];
var url = opt.value;
if (!url) return;
var linkTarget = opt.getAttribute('target') || target; // option 優先
switch (linkTarget) {
case '_blank':
window.open(url);
break;
case '_top':
window.top.location.href = url;
break;
case '_parent':
window.parent.location.href = url;
break;
default:
// 嘗試作為 iframe name
var frame = window.frames[linkTarget];
if (frame) {
frame.location.href = url;
} else {
window.location.href = url; // 當前窗口
}
}
if (restore) selectObj.selectedIndex = 0;
}
<noscript>
<ul>
<li><a href="https://www.fudan.org.cn/">復旦大學校友網</a></li>
<li><a href="https://www.pku.org.cn/">北京大學校友網</a></li>
</ul>
</noscript>
<select id="siteSelect">
<option value="">請選擇</option>
<option value="https://fudan.org.cn">復旦</option>
<option value="https://pku.org.cn">北大</option>
</select>
<script>
document.getElementById('siteSelect').addEventListener('change', function() {
var url = this.value;
if (url) window.location.href = url; // 或 window.open(url)
// 可選:this.selectedIndex = 0; // 重置
});
</script>
下拉跳轉是經典導航模式,核心只需 `onchange="location.href=this.value"`。生產環境建議:分離行為層、支持 target 屬性、提供無腳本降級、注意 SEO(搜索引擎不執行 JS 跳轉,重要鏈接應有 `` 版本)。