当前位置:编程学习 > JS >>

jquery 折叠菜单代码

;(function($) {

function load(settings, root, child, container) {
 $.getJSON(settings.url, {root: root}, function(response) {
  function createNode(parent) {
   var current = $("<li/>").attr("id", this.id || "").html("<span>" + this.text + "</span>").appendTo(parent);
   if (this.classes) {
    current.children("span").addClass(this.classes);
   }
   if (this.expanded) {
    current.addClass("open");
   }
   if (this.hasChildren || this.children && this.children.length) {
    var branch = $("<ul/>").appendTo(current);
    if (this.hasChildren) {
     current.addClass("hasChildren");
     createNode.call({
      text:"placeholder",
      id:"placeholder",
      children:[]
     }, branch);
    }
    if (this.children && this.children.length) {
     $.each(this.children, createNode, [branch])
    }
   }
  }
  $.each(response, createNode, [child]);
        $(container).treeview({add: child});
    });
}

var proxied = $.fn.treeview;
$.fn.treeview = function(settings) {
 if (!settings.url) {
  return proxied.apply(this, arguments);
 }
 var container = this;
 load(settings, "source", this, container);
 var userToggle = settings.toggle;
 return proxied.call(this, $.extend({}, settings, {
  collapsed: true,
  toggle: function() {
   var $this = $(this);
   if ($this.hasClass("hasChildren")) {
    var childList = $this.removeClass("hasChildren").find("ul");
    childList.empty();
    load(settings, this.id, childList, container);
   }
   if (userToggle) {
    userToggle.apply(this, arguments);
   }
  }
 }));
};

})(jQuery);

补充:网页制作,jquery
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,