这里是新的。我正在尝试使用onclick切换出一些我已分配给脚本中变量的图像,但我无法使其正常工作。有什么想法吗?
let doorImage1 = document.getElementById("door1");
let botDoorPath ='https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/robot.svg';
doorImage1.onclick= () =>{
doorImage1.src = botDoorPath;
}
答案 0 :(得分:0)
直到我先将元素中的src设置为某项,这对我来说都是无效的。似乎正在工作,所以也许找不到您的元素。在控制台上查看它是否未定义。
let doorImage1 = document.getElementById("door1");
let botDoorPath = 'https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/robot.svg';
doorImage1.onclick = () => {
console.log('click')
doorImage1.src = botDoorPath;
}
<html>
<img src='./missing.png' id='door1'></img>
</html>