Advertisement
regergr

Untitled

Dec 27th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Int</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript" src="jquery-3.3.1.min.js"></script>
  9. <script>
  10. var data = [
  11. {
  12. text: 'Животные',
  13. children: [
  14. {
  15. text: 'Млекопитающий',
  16. children: [
  17. {
  18. text: 'Коровы',
  19. children: []
  20. },
  21. {
  22. text: 'Ослы',
  23. children: []
  24. },
  25. {
  26. text: 'Собаки',
  27. children: []
  28. },
  29. {
  30. text: 'Тигры',
  31. children: []
  32. }
  33. ]
  34. },
  35. {
  36. text: 'Другие',
  37. children: [
  38. {
  39. text: 'Змеи',
  40. children: []
  41. },
  42. {
  43. text: 'Птицы',
  44. children: []
  45. },
  46. {
  47. text: 'Ящерицы',
  48. children: []
  49. }
  50. ]
  51. }
  52. ]
  53. },
  54. {
  55. text: 'Рыбы',
  56. children: [
  57. {
  58. text: 'Аквариумные',
  59. children: [
  60. {
  61. text: 'Гуппи',
  62. children: []
  63. },
  64. {
  65. text: 'Скалярии',
  66. children: []
  67. }
  68. ]
  69. },
  70. {
  71. text: 'Морские',
  72. children: [
  73. {
  74. text: 'Морская форель',
  75. children: []
  76. }
  77. ]
  78. }
  79. ]
  80. }];
  81. function recCreate(obj)
  82. {
  83. var element = $("<li>");
  84. element.text(obj.text)
  85.  
  86. if (obj.children.length != 0)
  87. {
  88. var elementUL = $("<ul>");
  89. for (var i = 0; i < obj.children.length; i++)
  90. elementUL.append(recCreate(obj.children[i]))
  91.  
  92. element.append(elementUL)
  93. }
  94. return element;
  95. }
  96. var element = $("<ul>");
  97. for (var i = 0; i < data.length; i++)
  98. element.append(recCreate(data[i]))
  99. $('body').append(element);
  100. $("li").click(function(e){
  101. e.stopPropagation();
  102. $(this).children("ul").slideToggle('slow');
  103. });
  104. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  105. $( "li" ).each(function() {
  106. var t = $(this).clone().children("ul").remove().end();
  107. console.log( "Text: " + t.text() + "\r\nCount li: " + $(this).find("li").length );
  108. });
  109. </script>
  110. </body>
  111. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement