exit() 与 remove() 的区别

先进行remove()后会删除掉节点,但是selection还会存在。
先exit() 将会清楚掉selection里面的内容,在调用remove()将不会删除掉节点。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>d3 5.15.1</title>
  5. <script src="../lib/d3.js" type="text/javascript"></script>
  6. <style>
  7. .text {
  8. text-indent: 2em;
  9. }
  10. .pre {
  11. white-space: pre-wrap;
  12. background-color: #eee;
  13. border: 1px solid #ccc;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <a href="../index.html">首页</a>
  19. <script>
  20. var matrix = [
  21. [11975, 5871, 8916, 2868],
  22. [1951, 10048, 2060, 6171],
  23. [8010, 16145, 8090, 8045],
  24. [1013, 990, 940, 6907]
  25. ];
  26. var body = d3.select("body");
  27. /* body = {
  28. _groups: Array(1)
  29. 0: [body]
  30. length: 1
  31. _parents: [html]
  32. } */
  33. var table = body.append("table");
  34. /* table = {
  35. _groups: Array(1)
  36. 0: [table]
  37. length: 1
  38. _parents: [html]
  39. } */
  40. var tr = table.selectAll("tr");
  41. /* tr = {
  42. _groups: [NodeList(0)]
  43. _parents: [table]
  44. } */
  45. var tr1 = tr.data(matrix);
  46. /* tr1 = {
  47. _groups: Array(1)
  48. 0: (4) [empty × 4]
  49. length: 1
  50. _parents: [table]
  51. _enter: Array(1)
  52. 0: (4) [EnterNode, EnterNode, EnterNode, EnterNode]
  53. length: 1
  54. _exit: Array(1)
  55. 0: []
  56. length: 1
  57. } */
  58. var tr2 = tr1.enter();
  59. /* tr2 = {
  60. _groups: Array(1)
  61. 0: Array(4)
  62. 0: EnterNode {ownerDocument: document, namespaceURI: "http://www.w3.org/1999/xhtml", _next: null, _parent: table, __data__: Array(4)}
  63. 1: EnterNode {ownerDocument: document, namespaceURI: "http://www.w3.org/1999/xhtml", _next: null, _parent: table, __data__: Array(4)}
  64. 2: EnterNode {ownerDocument: document, namespaceURI: "http://www.w3.org/1999/xhtml", _next: null, _parent: table, __data__: Array(4)}
  65. 3: EnterNode {ownerDocument: document, namespaceURI: "http://www.w3.org/1999/xhtml", _next: null, _parent: table, __data__: Array(4)}
  66. length: 4
  67. length: 1
  68. _parents: [table]
  69. } */
  70. var tr3 = tr2.append("tr");
  71. /* tr3 = {
  72. _groups: Array(1)
  73. 0: Array(4)
  74. 0: tr: { 类型HTMLElement
  75. __data__: [11975, 5871, 8916, 2868],
  76. nodeType: 1
  77. }
  78. 1: tr
  79. 2: tr
  80. 3: tr
  81. length: 4
  82. length: 1
  83. _parents: [table]
  84. } */
  85. var td = tr3.selectAll("td");
  86. /*
  87. td = {
  88. _groups: (4) [NodeList(0), NodeList(0), NodeList(0), NodeList(0)]
  89. _parents: (4) [tr, tr, tr, tr]
  90. }
  91. */
  92. var td1 = td.data(function (d) { return d; });
  93. /*
  94. td1 = {
  95. _groups: (4) [Array(4), Array(4), Array(4), Array(4)]
  96. 0: (4) [empty × 4]
  97. 1: (4) [empty × 4]
  98. 2: (4) [empty × 4]
  99. 3: (4) [empty × 4]
  100. _parents: (4) [tr, tr, tr, tr]
  101. _enter: (4) [Array(4), Array(4), Array(4), Array(4)]
  102. _exit: (4) [Array(0), Array(0), Array(0), Array(0)]
  103. }
  104. */
  105. var td2 = td1.enter().append("td");
  106. /*
  107. td2 = {
  108. _groups: Array(4)
  109. 0: (4) [td, td, td, td]
  110. 1: (4) [td, td, td, td]
  111. 2: (4) [td, td, td, td]
  112. 3: (4) [td, td, td, td]
  113. length: 4
  114. _parents: (4) [tr, tr, tr, tr]
  115. }
  116. */
  117. var td3 = td2.text(function (d) { return d; });
  118. /*
  119. td3 = {
  120. _groups: Array(4)
  121. 0: (4) [td, td, td, td]
  122. 1: (4) [td, td, td, td]
  123. 2: (4) [td, td, td, td]
  124. 3: (4) [td, td, td, td]
  125. length: 4
  126. _parents: (4) [tr, tr, tr, tr]
  127. }
  128. */
  129. var td6 = td3.remove();
  130. /*
  131. td6 = {
  132. _groups: Array(4)
  133. 0: (4) [td, td, td, td]
  134. 1: (4) [td, td, td, td]
  135. 2: (4) [td, td, td, td]
  136. 3: (4) [td, td, td, td]
  137. length: 4
  138. _parents: (4) [tr, tr, tr, tr]
  139. }
  140. */
  141. var td7 = td6.exit();
  142. /*
  143. td7 = {
  144. _groups: Array(4)
  145. 0: (4) [empty × 4]
  146. 1: (4) [empty × 4]
  147. 2: (4) [empty × 4]
  148. 3: (4) [empty × 4]
  149. length: 4
  150. _parents: (4) [tr, tr, tr, tr]
  151. }
  152. */
  153. // var td4 = td3.exit();
  154. /*
  155. td4 = {
  156. _groups: Array(4)
  157. 0: (4) [empty × 4]
  158. 1: (4) [empty × 4]
  159. 2: (4) [empty × 4]
  160. 3: (4) [empty × 4]
  161. length: 4
  162. _parents: (4) [tr, tr, tr, tr]
  163. }
  164. */
  165. // var td5 = td4.remove();
  166. /*
  167. td5 = {
  168. _groups: Array(4)
  169. 0: (4) [empty × 4]
  170. 1: (4) [empty × 4]
  171. 2: (4) [empty × 4]
  172. 3: (4) [empty × 4]
  173. length: 4
  174. _parents: (4) [tr, tr, tr, tr]
  175. }
  176. */
  177. </script>
  178. </body>
  179. </html>