2013-05-28 100 views
1

我想让我的导航栏与调整大小一起工作。 当屏幕尺寸缩小时,它会转换为选择选项。
它的工作原理,但问题是当屏幕尺寸减小时,第一个选项显示在选择框中而不是当前选项。通过jquery的响应式导航

我希望选择框留在当前选定的选项。


这里是我的代码

jQuery('<select />').appendTo('#navigation'); 

jQuery('#navigation a').each(function() { 
    var el = jQuery(this); 
    jQuery('<option />', { 
     "value": el.attr("href"), 
      "text": el.text() 
    }).appendTo('#navigation select'); 
}); 

jQuery('#navigation select').change(function() { 
    window.location = jQuery(this).find("option:selected").val(); 
}); 

有没有在我的jQuery代码的任何故障?请帮我解决这个问题

回答

0

试试这个:(假设链路级的“选择”激活时)

jQuery('<select />').appendTo('#navigation'); 

jQuery('#navigation a').each(function() { 
    var el = jQuery(this); 
    var current = jQuery('<option />', { 
     "value": el.attr("href"), 
      "text": el.text() 
    }); 

    url = document.URL.split('/'); 
    if(url[url.length-1] == current.attr('value')) 
     current.prop('selected', true); 
    current.appendTo('#navigation select'); 
}); 

jQuery('#navigation select').change(function() { 
    window.location = jQuery(this).find("option:selected").val(); 
}); 

对于第二个模板(删除<select>你已经到了那里):

jQuery('<select />').appendTo('#navigation'); 

jQuery('#menu-new-main-menu li a').each(function() { 
    var el = jQuery(this); 
    var current = jQuery('<option />', { 
     "value": el.attr("href"), 
      "text": el.text() 
    }); 

    if(document.URL == current.attr('value')) 
     current.prop('selected', true); 
    current.appendTo('#navigation select'); 
}); 

jQuery('#navigation select').change(function() { 
    window.location = jQuery(this).find("option:selected").val(); 
}); 
+0

在选择时没有选择的类添加到选项字段中。我想这是主要问题。我如何添加选定的类到当前选项? – Addicted

+0

你可以添加一个链接到页面,让我可以看到那里发生了什么?你不是不同的风格当前页面链接,然后剩下的? –

+0

我实际上在本地主机上工作,但是我正在使用此模板 - http://webcodebuilder.com/envato/showcase/blog.html – Addicted