jquery - 将选择列表函数从 "Remove"更改为 "Add To"

标签 jquery html html-lists optgroup

我有一个脚本,可以从 ul 列表中生成一个 opt 组选择,然后替换它。我想更改生成选择列表的函数,但不替换 ul 列表。这是 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> 

还有 JS:

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();​

我对 js 不太有经验,但我想删除

var sitemapNode;

$("#sitemap").removeAttr("id").after('<select id="sitemap" />').children().each(sitemapCycle).parent().remove();​

可以解决这个问题,但事实并非如此。这是一个实例:http://jsfiddle.net/XvruF/

最佳答案

将最后一行更改为:

$("#sitemap").removeAttr("id").after('<select id="sitemap" />').children().each(sitemapCycle);​

关于jquery - 将选择列表函数从 "Remove"更改为 "Add To",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13648069/

相关文章:

javascript - 使用键盘制表键 tabindex 元素未显示

jquery - CSS :hover not working for me in doctype

javascript - 在 Javascript/Jquery 中传递值

javascript - Chrome jQuery AJAX 失败,不是跨域问题

php - CodeIgniter:如何将用户当前所在页面的链接设为 'highlight'?

python - pandas DataFrame 到带有内联样式而不是 CSS 的 html

javascript - 同时定位无序列表和无序列表外的元素

html - 在元素之间添加空间,使背景不水平拉伸(stretch)

javascript 在 url 中传递一个变量,然后选择选择选项

javascript - jQuery:当jQuery对象找不到指定的类选择器时,jQuery返回什么?