gpt4 book ai didi

javascript - 为什么追加不起作用? this.append 不是一个函数

转载 作者:行者123 更新时间:2023-12-02 22:14:26 25 4
gpt4 key购买 nike

当我将绿色框拖放到另一个列中时,出现此错误:

this.append is not a function

为什么?

https://jsfiddle.net/gdxwks0t/1/

const cols = document.getElementsByClassName('col')
const item = document.querySelector('.item')

for (const col of cols) {
col.addEventListener("dragover", (e) => {
e.preventDefault()
})
col.addEventListener("drop", () => {
alert(this)
this.append(item)
})
}
<div class="col">
<div draggable="true" class="item"></div>
</div>
<div class="col"></div>
<div class="col"></div>

最佳答案

Arrow (=>) function expressions

An arrow function expression is a syntactically compact alternative to a regular function expression, although without its own bindings to the this, arguments, super, or new.target keywords.

改用普通函数语法。

const cols = document.getElementsByClassName('col')
const item = document.querySelector('.item')

for (const col of cols) {
col.addEventListener("dragover", (e) => {
e.preventDefault()
})
col.addEventListener("drop", function() {
alert(this)
this.append(item)
})
}
body {
background: #ccc;
}
.item {
background: #6dc06d;
width: 50px;
height: 50px;
margin: 10px;
}
.col {
float: left;
width: 70px;
height: 70px;
background: white;
margin: 10px;
}
<div class="col">
<div draggable="true" class="item"></div>
</div>
<div class="col"></div>
<div class="col"></div>

关于javascript - 为什么追加不起作用? this.append 不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59436359/

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