
这里演示垂直的树形菜单,应用CSS和jquery技术来实现,显示在网页左侧比较合适,考虑到简洁,没有使用图片之类的作修饰。本效果兼容IE、火狐等主流浏览器。
运行效果截图如下:

在线演示地址如下:
http://demo.jb51.net/js/2015/jquery-css-nlevel-vshow-tree-codes/
具体代码如下:
jQuery多级层叠树形菜单效果 script> (function($){ $.fn.extend({ accordion: function(options) { var defaults = { accordion: 'true', speed: 300, closedSign: '[+]', openedSign: '[-]' }; var opts = $.extend(defaults, options); var $this = $(this); $this.find("li").each(function() { if($(this).find("ul").size() != 0){ $(this).find("a:first").append(""+ opts.closedSign +""); if($(this).find("a:first").attr('href') == "#"){ $(this).find("a:first").click(function(){return false;}); } } }); $this.find("li.active").each(function() { $(this).parents("ul").slideDown(opts.speed); $(this).parents("ul").parent("li").find("span:first").html(opts.openedSign); }); $this.find("li a").click(function() { if($(this).parent().find("ul").size() != 0){ if(opts.accordion){ if(!$(this).parent().find("ul").is(':visible')){ parents = $(this).parent().parents("ul"); visible = $this.find("ul:visible"); visible.each(function(visibleIndex){ var close = true; parents.each(function(parentIndex){ if(parents[parentIndex] == visible[visibleIndex]){ close = false; return false; } }); if(close){ if($(this).parent().find("ul") != visible[visibleIndex]){ $(visible[visibleIndex]).slideUp(opts.speed, function(){ $(this).parent("li").find("span:first").html(opts.closedSign); }); } } }); } } if($(this).parent().find("ul:first").is(":visible")){ $(this).parent().find("ul:first").slideUp(opts.speed, function(){ $(this).parent("li").find("span:first").delay(opts.speed).html(opts.closedSign); }); }else{ $(this).parent().find("ul:first").slideDown(opts.speed, function(){ $(this).parent("li").find("span:first").delay(opts.speed).html(opts.openedSign); }); } } }); } }); })(jQuery); script>
希望本文所述对大家的jquery程序设计有所帮助。
