问题
I'm trying to convert (with jQuery) a multi-level UL
into a SELECT
dropdown with the nested UL
group being wrapped in OPTGROUPS
s. I'm messing around with the idea of using this technique to make responsive site menus (think drop-down menus).
I've converted list items into a option dropdown in the past, but never with optgroups, and I can't figure it out.
An example of my UL
structure;
<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>
Example of what I what it to become;
<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>
It doesn't need to go deeper than one level - and I'm pretty sure optgroups don't really work so well that deep anyways. Any assistance you can contribute would be very welcome. Thanks.
回答1:
Something simple solution for this
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(); });
回答2:
You can replace :
$("#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();
Or build and then remove the 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();
回答3:
// #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(); });
来源:https://stackoverflow.com/questions/6797684/convert-ul-to-select-w-optgroups