我对此JavaScript方法有问题。我只是希望当我点击一个链接时,它会显示隐藏的图片,当我再次单击它时,它会隐藏它,等等。但它不能正常工作。这是代码检查它自己并尝试帮助我处理这件事情。最好的祝福。JavaScript未显示隐藏图片
<head>
<style>
.hide {
display: none;
}
</style>
</head>
<body>
<a data-img='sloth-pic' id='sloth' href='#'>Sloth</a>
<img class='hide' id='sloth-pic' src='https://static-secure.guim.co.uk/sys-images/Education/Pix/pictures/2013/1/17/1358446759827/A-three-toed-tree-sloth-h-008.jpg' style='width:304px;height:228px;'>
<script>
var sloth = document.getElementById("sloth");
var slothPic = document.getElementById("sloth");
sloth.addEventListener("click", function() {
if(slothPic.className === "hide") {
sloth.className = "";
} else if(sloth.className === ""){
slothPic.className = "hide";
}
});
</script>
</body>
作为一个方面说明我会建议你看看jQuery的,因为这将让这样的事情容易得多。 – Oisin