简体   繁体   English

jQuery下拉列表选择的项目

[英]JQuery dropdown list selected item

I have a jquery drop down menu for small screens. 我有一个用于小屏幕的jquery下拉菜单。 I use the following code for that. 我使用以下代码。

  $("<select />").appendTo("nav");
     // Create default option "Go to..."
     $("<option />", {
         "selected": "selected",
         "value": "",
         "text": "Go to..."
     }).appendTo("nav select");
     // Populate dropdown with menu items
     $("nav a").each(function () {
         var el = $(this);
         $("<option />", {
             "value": el.attr("href"),
             "text": el.text()
         }).appendTo("nav select");
     });
     $("nav select").change(function () {
         window.location = $(this).find("option:selected").val();
     });

my problem is that, when menu item navigated to another page, that time also menu selected is always the first list item. 我的问题是,当菜单项导航到另一个页面时,那个时候所选菜单始终是第一个列表项。 For example if i select contact page from the dropdown list, it goes to contact page and shows the selected dropdown item as "Goto". 例如,如果我从下拉列表中选择联系人页面,它将转到联系人页面,并将所选的下拉项显示为“转到”。 How can i solve this problem. 我怎么解决这个问题。

js js

$(document).ready(function () {
    loc = $(location).attr('href');
    var n = loc.split("/");
    var n1 = loc.split("/").length;
    var on_page = n[n1 - 1];
    var new_page = on_page.split("?");
    $('nav ul a').each(function () {
        if ($(this).attr('href') == new_page[0]) {
            $(this).find('li').addClass('active');
        }
    });
});

css 的CSS

.active {
    background:#ffca00;
    color:#2e2f34;
    padding:16px 12px;
}

Another code if you are working with # in the href 如果您在href中使用#,则需要另一个代码

js js

$(document).ready(function () {
    $('nav ul li').click(function () {
        $(this).parent().parent().find('.active').removeClass('active');
        $(this).addClass('active');
    });
});

Working Demo http://jsfiddle.net/XEy4s/ 工作演示http://jsfiddle.net/XEy4s/

Try this (place after the .each() loops): 尝试以下操作(放置在.each()循环之后):

$("nav select").val(window.location.href);

the above should work just fine, but here's another way to do it: 上面的方法应该可以正常工作,但是这是另一种方法:

var currentPage = window.location.href;
$("<select />").appendTo("nav");

// Create default option "Go to..."
$("<option />", {
    "selected": "selected",
        "value": "",
        "text": "Go to..."
}).appendTo("nav select");

// Populate dropdown with menu items
$("nav a").each(function (i, elem) {
    var el = $(this);
    var href = el.attr("href");
    $("<option />", {
        "value": href,
            "text": el.text()
    }).appendTo("nav select");

    if(href == currentPage) {
        currentPage = i+1; // +1 because of additional 'Go to' option
    }
});

// Set selected index to current page
$("nav select")[0].selectedIndex = currentPage;

$("nav select").change(function () {
    window.location = $(this).find("option:selected").val();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM