简体   繁体   English

事件委托,Javascript中事件Target的使用

[英]Event delegation, use of event Target in Javascript

This is the html code:这是html代码:

<table id="bagua-table">
<tr>
  <th colspan="3"><em>Bagua</em> Chart: Direction, Element, Color, Meaning</th>
</tr>
<tr>
  <td class="nw"><strong>Northwest</strong>
    <br>Metal
    <br>Silver
    <br>Elders
  </td>
  <td class="n"><strong>North</strong>
    <br>Water
    <br>Blue
    <br>Change
  </td>
  <td class="ne"><strong>Northeast</strong>
    <br>Earth
    <br>Yellow
    <br>Direction
  </td>
</tr>
<tr>
  <td class="w"><strong>West</strong>
    <br>Metal
    <br>Gold
    <br>Youth
  </td>
  <td class="c"><strong>Center</strong>
    <br>All
    <br>Purple
    <br>Harmony
  </td>
  <td class="e"><strong>East</strong>
    <br>Wood
    <br>Blue
    <br>Future
  </td>
</tr>
<tr>
  <td class="sw"><strong>Southwest</strong>
    <br>Earth
    <br>Brown
    <br>Tranquility
  </td>
  <td class="s"><strong>South</strong>
    <br>Fire
    <br>Orange
    <br>Fame
  </td>
  <td class="se"><strong>Southeast</strong>
    <br>Wood
    <br>Green
    <br>Romance
  </td>
</tr>

The script is to make any selected cell highlight and if clicked again, remove the highlight, What's the use of the selectedTd variable and the line that contains target.parentnode , what do those two lines do?该脚本是使任何选定的单元格突出显示,如果再次单击,则删除突出显示, selectedTd变量和包含target.parentnode的行有什么用处,这两行有什么作用?

This is the Javascript code:这是Javascript代码:

let table = document.getElementById('bagua-table');

let selectedTd;

table.onclick = function(event) {
  let target = event.target;

  while (target != this) {
    if (target.tagName == 'TD') {
      highlight(target);
      return;
    }
    target = target.parentNode;
  }
}

function highlight(node) {
  if (selectedTd) {
    selectedTd.classList.remove('highlight');
  }
  selectedTd = node;
  selectedTd.classList.add('highlight');//
}

target.parentNode will get the parent node that the current node belongs to. target.parentNode将获取当前节点所属的父节点。 The line is setting the target to be the HTML element which holds the current element.该行将target设置为包含当前元素的 HTML 元素。

Eg <div class="parent"><button class="child" /></div> .例如<div class="parent"><button class="child" /></div> If target was the button , then the parent node would be the div .如果targetbutton ,那么父节点就是div

This is happening every loop until a td element is found, or it reaches the global object.每次循环都会发生这种情况,直到找到td元素,或者它到达全局对象。

selectedTd is used to keep a state of what the currently selected cell is. selectedTd用于保持当前选定单元格的状态。 Every time the highlight function is called, it'll check if there is a selectedTd that already exists.每次调用highlight函数时,它都会检查是否存在已存在的selectedTd The existing one will have its highlight class removed and will be replaced by the node passed into the function.现有的将删除其highlight类,并由传递到函数的node替换。 This node will have the highlight class added.node将添加highlight类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM