html - 通过 html select 更改 URL

标签 html url url-rewriting html-select

通过 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 Mozillahere 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/

相关文章:

javascript - 在菜单链接中使用 # ids 时,Vue Js 历史记录模式不会删除 #

sql - 如何更改 magento 中的 base_url。因为我的网站在我的 magento 管理员上更改后消失了

Spring Web Flow 和 Url Rewrite Filter——清理 URL

php - Facebook 用户名如何运作?

html - 我怎样才能在导航栏中居中链接和其他右/左定位的链接?

html - 我的内联图像下方的额外间距

javascript - 如何在 HTML5 Canvas 中的两个文本对象之间绘制二次曲线?

iphone - 如何从我的 iPhone 应用程序中打开 Twitter 应用程序中的 Twitter 页面?

apache - .htaccess 将子域重写到目录

html - 使样式显示在 p 标签之前