Create <select> from list - indent child items?

柔情痞子 提交于 2019-12-04 18:37:00

This should do what you want, and it's pretty customizable. http://jsfiddle.net/C5S32/7/

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

As a side note, in your code you're appending stuff many times. It's better to append everything in one call to avoid excessive reflows.

This should do it, Not sure about support for "margin-left" on mobiles but will point you in the right direction.

http://jsfiddle.net/C5S32/2/

EDIT: Seems only Firefox allows margin on option elements,

http://jsfiddle.net/C5S32/6/

See if this suits your needs:

http://jsfiddle.net/C5S32/1/

I just added this:

li {
    padding-left: 1em;
}​

Best Solution that I found adds child indented "-" marks automatically. Make sure that you change the top ".navbar .nav" to your own div class or id that is setup on the original ul.

http://snipplr.com/view/66721/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!