我正在用 html 构建网页。当我将鼠标悬停在一个 div 上时,图像必须改变,但它什么也没做。有人知道吗?
<!doctype html>
<html>
<head>
<meta charset ="UTF-8"/>
<title>Oef 19.5</title>
<link href="19_5.css" rel="stylesheet"/>
<script>
function kleuren(veld)
{
var tshirt = document.getElementsByClassName("shirt");
tshirt.src=veld.id+".jpg";
}
</script>
</head>
<body>
<h1>Pick a color</h1>
<p><img class="shirt" src="gray.jpg"></p>
<div id="pink" class="roze" onmouseover="kleuren(this)"></div>
<div id="blue" class="blauw" onmouseover="kleuren(this)"></div>
<div id="brown" class="bruin" onmouseover="kleuren(this)"></div>
</body>
</html>
最佳答案
var tshirt = document.getElementsByClassName("shirt");
返回一个元素集合,因此可以像这样访问它
tshirt[0].src=veld.id+".jpg"; //Since you have only one element with that class. If you have more, iterate over them
关于javascript - 在javascript中更改鼠标悬停的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30482436/