Advertisement
vitareinforce

event delegation

Aug 17th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. <!DOCTYPE HTML>
  2. <html>
  3.  
  4. <body>
  5. <style>
  6. #bagua-table th {
  7. text-align: center;
  8. font-weight: bold;
  9. }
  10.  
  11. #bagua-table td {
  12. width: 150px;
  13. white-space: nowrap;
  14. text-align: center;
  15. vertical-align: bottom;
  16. padding-top: 5px;
  17. padding-bottom: 12px;
  18. }
  19.  
  20. #bagua-table .nw {
  21. background: #999;
  22. }
  23.  
  24. #bagua-table .n {
  25. background: #03f;
  26. color: #fff;
  27. }
  28.  
  29. #bagua-table .ne {
  30. background: #ff6;
  31. }
  32.  
  33. #bagua-table .w {
  34. background: #ff0;
  35. }
  36.  
  37. #bagua-table .c {
  38. background: #60c;
  39. color: #fff;
  40. }
  41.  
  42. #bagua-table .e {
  43. background: #09f;
  44. color: #fff;
  45. }
  46.  
  47. #bagua-table .sw {
  48. background: #963;
  49. color: #fff;
  50. }
  51.  
  52. #bagua-table .s {
  53. background: #f60;
  54. color: #fff;
  55. }
  56.  
  57. #bagua-table .se {
  58. background: #0c3;
  59. color: #fff;
  60. }
  61.  
  62. #bagua-table .highlight {
  63. background: red;
  64. }
  65. </style>
  66.  
  67.  
  68. <table id="bagua-table">
  69. <tr>
  70. <th colspan="3"><em>Bagua</em> Chart: Direction, Element, Color, Meaning</th>
  71. </tr>
  72. <tr>
  73. <td class="nw"><strong>Northwest</strong>
  74. <br>Metal
  75. <br>Silver
  76. <br>Elders
  77. </td>
  78. <td class="n"><strong>North</strong>
  79. <br>Water
  80. <br>Blue
  81. <br>Change
  82. </td>
  83. <td class="ne"><strong>Northeast</strong>
  84. <br>Earth
  85. <br>Yellow
  86. <br>Direction
  87. </td>
  88. </tr>
  89. <tr>
  90. <td class="w"><strong>West</strong>
  91. <br>Metal
  92. <br>Gold
  93. <br>Youth
  94. </td>
  95. <td class="c"><strong>Center</strong>
  96. <br>All
  97. <br>Purple
  98. <br>Harmony
  99. </td>
  100. <td class="e"><strong>East</strong>
  101. <br>Wood
  102. <br>Blue
  103. <br>Future
  104. </td>
  105. </tr>
  106. <tr>
  107. <td class="sw"><strong>Southwest</strong>
  108. <br>Earth
  109. <br>Brown
  110. <br>Tranquility
  111. </td>
  112. <td class="s"><strong>South</strong>
  113. <br>Fire
  114. <br>Orange
  115. <br>Fame
  116. </td>
  117. <td class="se"><strong>Southeast</strong>
  118. <br>Wood
  119. <br>Green
  120. <br>Romance
  121. </td>
  122. </tr>
  123.  
  124. </table>
  125.  
  126. <script>
  127. let table = document.getElementById('bagua-table');
  128.  
  129. let selectedTd;
  130.  
  131. table.onclick = function(event) {
  132. let target = event.target;
  133.  
  134. while (target != this) {
  135. if (target.tagName == 'TD') {
  136. highlight(target);
  137. return;
  138. }
  139. target = target.parentNode;
  140. }
  141. }
  142.  
  143. function highlight(node) {
  144. if (selectedTd) {
  145. selectedTd.classList.remove('highlight');
  146. }
  147. selectedTd = node;
  148. selectedTd.classList.add('highlight');
  149. }
  150. </script>
  151.  
  152. </body>
  153.  
  154. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement