简体   繁体   English

从选择菜单中删除嵌套选项 - jQuery

[英]Remove nested options from select menu - jQuery

I have a simple bit of Javascript. 我有一点简单的Javascript。

Is it possible to remove any nested parts of this menu when it becomes a select menu? 当它成为选择菜单时,是否可以删除此菜单的任何嵌套部分?

So in the demo below, the '- test' part wouldn't appear in the select menu. 因此,在下面的演示中,' - test'部分不会出现在选择菜单中。

DEMO: http://jsfiddle.net/ZBZRw/ 演示: http //jsfiddle.net/ZBZRw/

My jQuery is: 我的jQuery是:

$(function() {

            var options = '<option selected>Go to...</option>';
            $('nav div.alt').find('a').each(function () {
                var text = $(this).text(),
                purl = $(this).attr("href"),
                    depth = $(this).parent().parents('ul').length,
                    depthChar = '',
                    i = 1;
                for (i; i < depth; i++) { depthChar += '&ndash;&nbsp;'; }
                options += '<option value="' + purl + '">' + depthChar + text + '</option>';
            });
            $('<select />').append(options).appendTo('nav div.alt');

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

        });

Many thanks for any advice. 非常感谢任何建议。

Use the child selector > to be more exact in the <a> elements that you select 使用子选择器>在您选择的<a>元素中更精确

$('nav div.alt > ul > li > a').each(function () {

http://jsfiddle.net/ZBZRw/1/ http://jsfiddle.net/ZBZRw/1/

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

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