2 回答
TA贡献1911条经验 获得超7个赞
只需“dragstart”而不是“ondragstart”事件:
for (const character of result) {
let image = document.createElement("img");
image.src = character.src;
image.setAttribute('data-jval', character.jval);
image.setAttribute('id', character.id);
image.setAttribute('class', character.class)
image.setAttribute('draggable', 'true')
// the below function isn't being added to the divs
image.addEventListener('dragstart', drag);
let wrapper = document.createElement('div');
wrapper.appendChild(image);
section.appendChild(wrapper);
}
function drag(event) {
let data = event.target.dataset.jval;
event.dataTransfer.setData("text", data);
}
因此,基本上,当您编写内联事件处理程序时,您可以在事件之前添加前缀on,就像之前将事件处理程序放入标记中一样img。对于任何其他事件也是如此:
click => onclick
submit => onsubmit
keydown => onkeydown
TA贡献1830条经验 获得超3个赞
#try to add Semicolon;
image.setAttribute('class', character.class);
image.setAttribute('draggable', 'true');
// the below function isn't being added to the divs
image.addEventListener('ondragstart', function(event){
let data = event.target.dataset.jval;
event.dataTransfer.setData("text", data);
});
添加回答
举报