通过 html 选择更改 URL 的最佳方法是什么?
<select>
<option selected="selected">Change to URL X</option>
<option>Change to URL Y</option>
</select>
应该使用什么 Javascript?
最佳答案
<script type="text/javascript">
function navigateTo(sel, target, newWindow) {
var url = sel.options[sel.selectedIndex].value;
if (newWindow) {
window.open(url, target, '--- attributes here, see below ---');
} else {
window[target].location.href = url;
}
}
</script>
<select onchange="navigateTo(this, 'window', false);">
<option selected="selected" value="http://www.example.com/#X">Change to URL X</option>
<option value="http://www.example.com/#Y">Change to URL Y</option>
</select>
target
的一些有用值可能是 'window'
(当前窗口)或 'top'
(跳出框架集或 iframe)。如果你想打开一个新窗口,你可以使用 navigateTo(this, 'someWindow', true);
'--- attributes ---' 的值是使用记录的各种属性设置的 here for Mozilla和 here for IE .例如:
'height=300,width=400,top=100,left=100,statusbar=0,toolbar=1'
关于html - 通过 html select 更改 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1445263/