var image = document.getElementById("image");
function hide(el){
el.hidden = true;
}
image.onclick = hide(image);
我已经无数次地检查过这部分,但我不够聪明,看不出它为什么不起作用。感谢各位大佬的帮助。
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Chore Door!</title>
<link href="./style.css" rel="stylesheet" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Work+Sans" rel="stylesheet" type="text/css">
</head>
<body>
<div id="image"><img src="https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/closed_door.svg" id="image></div>
<script src="script.js"></script>
</body>
</html>
最佳答案
您的函数在页面加载时调用并执行。在匿名函数内部调用该函数。
请注意:您没有任何带有 id=image
的元素。
var image = document.getElementById("image");
function hide(el){
el.hidden = true;
}
image.onclick = function(){ hide(image) };
<div id="image"><img src="https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/closed_door.svg" id="image"></div>
也许下面的演示可以帮助您消除疑虑:
var image = document.getElementById("image");
function hide(){
image.hidden = true;
}
image.onclick = hide; // notice there is no () after the function name, here the function will not be executed on page load
<div id="image"><img src="https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/closed_door.svg" id="image"></div>
关于javascript - 我想在点击时隐藏图像。 element.onclick 不起作用时调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57053090/