0
I have a responsive menu on my site that some options should not appear when viewed through mobile.
<ul class="sf-menu">
<li><a href="#lk1">A gente!</a>
<ul>
<li><a href="#" id="btn-lk1-1">Como foi?</a></li>
<li><a href="#">O que falam?</a>
<ul>
<li><a href="#" id="btn-lk1-2-1">Dos projetos</a></li>
<li><a href="#" id="btn-lk1-2-2">Da EstruturaOnline</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#lk2">WWW o quê?</a></li>
<li><a href="#lk3">Vocês</a></li>
<li><a href="#lk4">Fizemos</a></li>
<li><a href="#lk5">Fazemos</a></li>
<li><a href="#lk6">Fale</a></li>
</ul>
Above I have the menu in list format, but no q appear sub nodes, IE, should only appear the first level of LI.
In the following code I have the jQuery that transforms this list into select, but it brings all second and third levels, of which I do not want...
(function($){
$.fn.mobileMenu = function(options) {
var defaults = {
defaultText: 'Navigate to...',
className: 'select-menu',
subMenuClass: 'sub-menu',
subMenuDash: '–'
},
settings = $.extend( defaults, options ),
el = $(this);
this.each(function(){
var $el = $(this),
$select_menu;
// ad class to submenu list
$el.find('ul').addClass(settings.subMenuClass);
// Create base menu
var $select_menu = $('<select />',{
'class' : settings.className + ' ' + el.get(0).className
}).insertAfter( $el );
// Create default option
$('<option />', {
"value" : '#',
"text" : settings.defaultText
}).appendTo( $select_menu );
// Create select option from menu
$el.find('a').each(function(){
var $this = $(this),
optText = ' ' + $this.text(),
optSub = $this.parents( '.' + settings.subMenuClass ),
len = optSub.length,
dash;
// if menu has sub menu
if( $this.parents('ul').hasClass( settings.subMenuClass ) ) {
dash = Array( len+1 ).join( settings.subMenuDash );
optText = dash + optText;
}
// Now build menu and append it
$('<option />', {
"value" : this.href,
"html" : optText,
"selected" : (this.href == window.location.href)
}).appendTo( $select_menu );
}); // End el.find('a').each
// Change event on select element
$select_menu.change(function(){
var locations = $(this).val();
if( locations !== '#' ) {
window.location.href = $(this).val();
};
});
$('.select-menu').show();
}); // End this.each
return this;
};
})(jQuery);
$(document).ready(function(){
$('.sf-menu').mobileMenu();
});
Can anyone help me with jQuery? I need only the first level of the list to appear when building select...
It would be something like
$("li [id^='lk']).children('ul').remove();
, nay?– Felipe Avelar
I with the line of code above, but ended up removing more data! Somehow I managed, I made some changes in both codes... If you want to see, please go to www.estruturaonline.com.br
– Danilo Rago
If possible post your answer here and select as a response, you can help other users.
– Felipe Avelar