【问题标题】:Convert UL to SELECT w/OPTGROUPs使用/OPTGROUP 将 UL 转换为 SELECT
【发布时间】:2011-10-11 11:29:56
【问题描述】:

我正在尝试(使用 jQuery)将多级 UL 转换为 SELECT 下拉列表,其中嵌套的 UL 组被包裹在 OPTGROUPSs 中。我正在搞乱使用这种技术制作响应式站点菜单(想想下拉菜单)的想法。

我过去曾将列表项转换为选项下拉列表,但从未使用 optgroups,我无法弄清楚。

我的UL 结构示例;

<ul id="sitemap">
    <li><a href="www.google.com">Home</a></li>
    <li><a href="www.google.com">Small Business</a>
        <ul>
            <li><a href="www.google.com">Laptops</a></li>
            <li><a href="www.google.com">Workstations</a></li>
            <li><a href="www.google.com">Workstations</a></li>
            <li><a href="www.google.com">Printers</a></li>
            <li><a href="www.google.com">Mobile Phones</a></li>
        </ul>
    </li>
    <li><a href="www.google.com">Public Sector</a>
        <ul>
            <li><a href="www.google.com">State Government</a></li>
            <li><a href="www.google.com">Federal Government</a></li>
            <li><a href="www.google.com">Support and Drivers</a></li>
        </ul>
    </li>
    <li><a href="www.google.com">Large Enterprise</a>
        <ul>
            <li><a href="www.google.com">Services</a></li>
            <li><a href="www.google.com">Solutions</a></li>
        </ul>
    </li>
</ul>

我会变成什么样子的例子;

<select id="sitemap">
    <option href="www.google.com">Home</option>
    <optgroup label="Small Business">
        <option href="www.google.com">Laptops</option>
        <option href="www.google.com">Workstations</option>
        <option href="www.google.com">Printers</option>
        <option href="www.google.com">Mobile Phones</option>
    </optgroup>
    <optgroup label="Public Sector">
        <option href="www.google.com">State Government</option>
        <option href="www.google.com">Federal Government</option>
        <option href="www.google.com">Support and Drivers</option>
    </optgroup>
    <optgroup label="Large Enterprise">
        <option href="www.google.com">Services</option>
        <option href="www.google.com">Solutions</option>
    </optgroup>
</select>

它不需要比一层更深 - 而且我很确定 optgroups 无论如何都不会真正工作得那么深。非常欢迎您提供任何帮助。谢谢。

【问题讨论】:

  • 您是否试图让当您有一个小的视口时,您的菜单变成一个下拉列表,以便在移动浏览器上有人可以通过触发下拉来导航?因为下拉菜单不一定能为用户提供更好的体验。

标签: jquery list select optgroup


【解决方案1】:
    // #ShankarSangoli code changed a little

var markUp = ["<select id='sitemap'>"], $li, $a;

$("#sitemap > li").each(function() {
    $li = $(this);
    if ($li.find("> ul")) {
        markUp.push("<optgroup label='"+$li.find(">a").text()+"'>");

        $li.find("li").each(function() {
            $a = $(this).find("a");
            markUp.push("<option value='"+$a.attr("href")+"'>"+$a.text()+"</option>");
        });

        markUp.push("</optgroup>");
    } else {
        $a = $li.find("a");
        markUp.push("<option value='"+$a.attr("href")+"'>"+$a.text()+"</option>")
    }
});

markUp.push("</select>");

$("#sitemap").replaceWith(markUp.join(''));
$("#sitemap").change(function(){ window.location = $(this).val(); });

【讨论】:

    【解决方案2】:

    一些简单的解决方案

    var markUp = ["<select id='sitemap'>"], $li, $a;
    $("#sitemap > li").each(function(){
       $li = $(this);
       if($li.find(">li").length){
         markUp.push("<optgroup label='"+$li.find(">a").text()+"'>"); 
         $li.find(">li").each(function(){
           $a = $(this).find("a");
           markUp.push("<option value='"+$a.attr("href")+"'>"+$a.text()+"</option>")
         });
         markUp.push("</optgroup>");
       }
       else{
         $a = $li.find("a");
         markUp.push("<option value='"+$a.attr("href")+"'>"+$a.text()+"</option>")
       }
    });
    markUp.push("</select>");
    
    $("#sitemap").replaceWith(markUp.join(''));
    $("#sitemap").change(function(){ window.location = $(this).val(); });
    

    【讨论】:

      【解决方案3】:

      你可以替换:

      $("#sitemap ul").each(function(){
         $(this).parent().replaceWith('<optgroup label="'+$(this).prev().text()+'">'+$(this).html().replace(/<li><a href="([^"]*)">([^<]*)<\/a><\/li>/g,'<option value="$1">$2</option>')+'</optgroup>');
      });
      
      $("#sitemap li").each(function(){
         $(this).replaceWith('<option value="'+$(this).children().attr('href')+'">'+$(this).text()+'</option>');
      });
      
      $("#sitemap").removeAttr("id").wrapInner('<select id="sitemap" />').children().unwrap();
      

      或者构建然后删除 ul#sitemap :

      function sitemapCycle(){
          if(typeof(sitemapNode)==="undefined") sitemapNode= $("#sitemap");
          if($(this).find("ul").length)
          {
              sitemapNode= $(sitemapNode).append('<optgroup label="'+$(this).children().first().text()+'" />').children().last();
              $(this).find("ul li").each(sitemapCycle);
              sitemapNode= $(sitemapNode).parent();
          }
          else
          {
               $(sitemapNode).append('<option value="'+$(this).children().attr("href")+'">'+$(this).text()+'</option>');
          }
      }
      
      var sitemapNode;
      
      $("#sitemap").removeAttr("id").after('<select id="sitemap" />').children().each(sitemapCycle).parent().remove();
      

      【讨论】:

      • 非常感谢@Enki,第二个解决方案对我来说效果很好。我最后才弹出这个; $("nav select").change(function() { window.location = $(this).find("option:selected").val(); }); 所以下拉选项将链接到其他页面。非常感谢!
      • 嘿亚伦,如果它对你有用,你绝对应该接受 Enki 的条目作为答案!
      猜你喜欢
      • 2013-05-14
      • 2023-03-21
      • 2014-05-14
      • 2013-01-27
      • 1970-01-01
      • 1970-01-01
      • 2013-05-26
      • 2023-04-02
      • 1970-01-01
      相关资源
      最近更新 更多