我有下一张桌子:



div class="container">
    <table id="myTable">
    <tr onmousemove="changeVposition(this)">
      <td onmousemove="changeGposition(this)">
        <div class="cell"></div>
      </td>
      <td onmousemove="changeGposition(this)">
        <div class="cell"></div>
      </td>
      <td onmousemove="changeGposition(this)">
        <div class="cell"></div>
      </td>
      <td onmousemove="changeGposition(this)">
        <div class="cell"></div>
      </td>
    </tr>
    <tr onmousemove="changeVposition(this)">
      <td onmousemove="changeGposition(this)">
        <div class="cell"></div>
      </td>
      <td onmousemove="changeGposition(this)">
        <div class="cell"></div>
      </td>
      <td onmousemove="changeGposition(this)">
        <div class="cell"></div>
      </td>
      <td onmousemove="changeGposition(this)">
        <div class="cell"></div>
      </td>
    </tr>
    <tr onmousemove="changeVposition(this)">
      <td onmousemove="changeGposition(this)">
        <div class="cell"></div>
      </td>
      <td onmousemove="changeGposition(this)">
        <div class="cell"></div>
      </td>
      <td onmousemove="changeGposition(this)">
        <div class="cell"></div>
      </td>
      <td onmousemove="changeGposition(this)">
        <div class="cell"></div>
      </td>
    </tr>
    <tr onmousemove="changeVposition(this)">
      <td onmousemove="changeGposition(this)">
        <div class="cell"></div>
      </td>
      <td onmousemove="changeGposition(this)">
        <div class="cell"></div>
      </td>
      <td onmousemove="changeGposition(this)">
        <div class="cell"></div>
      </td>
      <td onmousemove="changeGposition(this)">
        <div class="cell"></div>
      </td>
    </tr>

    </table>
    <button onclick="appendCol()" id ="appendCol"></button>
    <button onclick="appendRow()" id ="appendRow"></button>
    <button onclick="deleteRow()" id="deleteRow"></button>
    <button onclick="deleteCol()" id="deleteCol"></button>

  </div>





在创建具有功能的新列期间



function appendCol() {
      var tbl = document.getElementById("myTable")
      var tr = tbl.getElementsByTagName("tr");
      for (i = 0; i < tr.length; i++) {

        var td = document.createElement('td');
        td.onmousemove = changeGposition(this);
        var div = document.createElement('div')
        div.className = "cell"
        tr[i].appendChild(td).appendChild(div)



      }





但它不起作用,未添加onmousemove

谁知道为什么它不起作用?

注意:使用<div>元素可以正常工作,并且在调用函数后有下一个DOM



<td onmousemove="changeGposition(this)">
        <div class="cell"></div>
      </td>
<td>
        <div class="cell"></div>
      </td>

最佳答案

当你做

td.onmousemove = changeGposition(this);


它正在调用该函数并将其返回的值分配给事件侦听器。

它应该是

td.onmousemove = function () { changeGposition(this); }

关于javascript - 无法添加onmousemove,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44458447/

10-09 20:50