选择菜单,使用JQuery在选择时转到url? [英] Select Menu, go to url on select with JQuery?
本文介绍了选择菜单,使用JQuery在选择时转到url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下html:
HTML标记
<ul id="test">
<li><a href="http://www.yahoo.com">yahoo</a></li>
<li><a href="http://www.google.com">Google</a></li>
</ul>
和一些JS代码:
JQuery/JavaScript代码
$('ul#test').each(function()
{
var select=$(document.createElement('select')).insertBefore($(this).hide());
$('>li a', this).each(function()
{
option=$(document.createElement('option')).appendTo(select).val(this.href).html($(this).html());
});
});
此代码将产生一个选择下拉菜单,正是我想要的菜单,但是我的问题是如何在选择时转到URL?所以,如果我单击yahoo,它会将我带到yahoo.com吗?
This code produces a select dropdown menu, exactly what I want, but my question is how do I go to the url on select? So if I click yahoo, it brings me to yahoo.com?
感谢您的帮助!
推荐答案
$('ul#test').each(function()
{
var select=$(document.createElement('select')).insertBefore($(this).hide());
$('>li a', this).each(function()
{
option=$(document.createElement('option')).appendTo(select).val(this.href).html($(this).html());
});
select.change(function(){
//alert('url = ' + this.value );
window.location.href = this.value;
})
});
在主要浏览器上测试了演示的正常工作.
tested working demo on major browsers.
这篇关于选择菜单,使用JQuery在选择时转到url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文