-6ren">
gpt4 book ai didi

javascript - 无法添加 onmousemove

转载 作者:行者123 更新时间:2023-11-30 15:15:55 27 4
gpt4 key购买 nike

我有下一张 table :

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com