SimpleTree.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. Author:张浩华
  3. Date:2011.11.25 0:12
  4. Version:SimpleTree 1.0
  5. */
  6. $(function(){
  7. $.fn.extend({
  8. SimpleTree:function(options){
  9. //初始化参数
  10. var option = $.extend({
  11. click:function(a){ }
  12. },options);
  13. option.tree=this; /* 在参数对象中添加对当前菜单树的引用,以便在对象中使用该菜单树 */
  14. option._init=function(){
  15. /*
  16. * 初始化菜单展开状态,以及分叉节点的样式
  17. */
  18. this.tree.find("ul ul").hide(); /* 隐藏所有子级菜单 */
  19. this.tree.find("ul ul").prev("li").removeClass("open"); /* 移除所有子级菜单父节点的 open 样式 */
  20. this.tree.find("ul ul[show='true']").show(); /* 显示 show 属性为 true 的子级菜单 */
  21. this.tree.find("ul ul[show='true']").prev("li").addClass("open"); /* 添加 show 属性为 true 的子级菜单父节点的 open 样式 */
  22. }/* option._init() End */
  23. /* 设置所有超链接不响应单击事件 */
  24. this.find("a").click(function(){ $(this).parent("li").click(); return false; });
  25. /* 菜单项 <li> 接受单击 */
  26. this.find("li").click(function(){
  27. /*
  28. * 当单击菜单项 <li>
  29. * 1.触发用户自定义的单击事件,将该 <li> 标签中的第一个超链接做为参数传递过去
  30. * 2.修改当前菜单项所属的子菜单的显示状态(如果等于 true 将其设置为 false,否则将其设置为 true)
  31. * 3.重新初始化菜单
  32. */
  33. option.click($(this).find("a")[0]); /* 触发单击 */
  34. /*
  35. * 如果当前节点下面包含子菜单,并且其 show 属性的值为 true,则修改其 show 属性为 false
  36. * 否则修改其 show 属性为 true
  37. */
  38. if($(this).next("ul").attr("show")=="true"){
  39. $(this).next("ul").attr("show","false");
  40. }else{
  41. $(this).next("ul").attr("show","true");
  42. }
  43. /* 初始化菜单 */
  44. option._init();
  45. });
  46. /* 设置所有父节点样式 */
  47. this.find("ul").prev("li").addClass("folder");
  48. /* 设置节点“是否包含子节点”属性 */
  49. this.find("li").find("a").attr("hasChild",false);
  50. this.find("ul").prev("li").find("a").attr("hasChild",true);
  51. /* 初始化菜单 */
  52. option._init();
  53. }/* SimpleTree Function End */
  54. });
  55. });