html - multiple level unorderd list hierarchy in jquery -
i have html , css code, hve done in css show , hide, want done in jquery click event , padding left , right seccond , third level unroderd list has increased parent, if add 1 more unorderd list has add padding dynamically , shouldn't have add css that.
<ul class="delete"> <li><a>1</a> <ul> <li><a>1a</a></li> <li><a>1b</a></li> <li><a>1c</a></li> <li><a>1d</a></li> </ul> </li> <li><a>2</a> <ul> <li><a>2a</a></li> <li><a>2b</a></li> <li><a>2c</a></li> <li><a>2d</a></li> </ul> </li> <li><a>3</a> <ul> <li><a>3a</a> <ul> <li><a>3aa</a> <ul> <li><a>3aaa</a></li> <li><a>3aab</a></li> <li><a>3aac</a></li> <li><a>3aad</a></li> </ul> </li> <li><a>3ab</a></li> <li><a>3ac</a></li> <li><a>3ad</a></li> </ul> </li> <li><a>3b</a></li> <li><a>3c</a></li> <li><a>3d</a></li> </ul> </li> <li><a>4</a></li> </ul> .delete li a:hover { background-color: #008000; color: #ffffff; } ul { list-style:none; padding:0px; } .delete { width: 300px; } .delete > li { margin: 15px 0px; } .delete > li > { display: block; padding: 0px 10px; border: 2px solid #000000; } .delete > li > ul > li > { display: block; padding: 0px 20px; border: 2px solid #000000; } .delete > li > ul > li > ul > li > { display: block; padding: 0px 40px; border: 2px solid #000000; } .delete > li > ul > li > ul > li > ul > li > a{ display: block; padding: 0px 60px; border: 2px solid #000000; } .delete li ul { display: none; } .delete li:hover > ul{ display: block; }
here fiddle
it easier use plugin accomplish task. user @pawanlakhara suggested jqsimplemenu.
another possible solution use recursive function iterates on list items , sets css on each anchor element:
function iterateitems($context) { $context.children('ul').each(function(){ $(this).children('li').each(function(){ var $this = $(this); var p = $context.data('li-padding'); $this.data('li-padding', ((!p) ? 10 : p + 10)); $this.children('a:first').css({ 'display': 'block', 'padding': '0px ' + $this.data('li-padding') + 'px', 'border': '2px solid #000000' }); iterateitems($(this)); }); }); } iterateitems($('body'));
Comments
Post a Comment