大家好,我是该社区的新手,希望能有所帮助!
我在使用JavaScript的初学者水平!一直试图做到这一点,但似乎无法在网上找到任何东西,希望有人能帮助我!
<script>
window.addEventListener("load", function() {
var sImg1 = document.getElementById("img1").src = "images/image_1P.jpg";
// mouseover
sImg1.addEventListener("mouseover", function() {
sImg1.getElementById("myImg1").src = "images/image_1C.jpg";
})
//mouseout
sImg1.addEventListener("mouseout", function() {
sImg1.getElementById("myImg1").src = "images/image_1P.jpg";
})
})
</script>
<body>
<img class="img1" id="myImg1" src="" width="150" height="200">
我希望图像在鼠标悬停时从1p切换到1c,在鼠标悬停时从1c切换到1p!
最佳答案
您的脚本做什么和不做什么。
window.addEventListener("load", function() {
var sImg1 = document.getElementById("img1").src = "images/image_1P.jpg";
//sImg1 is a string which doesn't have events
// mouseover makes no sense (see above)
sImg1.addEventListener("mouseover", function() {
sImg1.getElementById("myImg1").src = "images/image_1C.jpg";
})
//mouseout
sImg1.addEventListener("mouseout", function() {
sImg1.getElementById("myImg1").src = "images/image_1P.jpg";
})
})
怎么修。
window.addEventListener("load", function() {
var sImg1 = document.getElementById("myImg1");
sImg1.src = "images/image_1P.jpg";
// mouseover
sImg1.addEventListener("mouseover", function() {
//this is an event source
this.src = "images/image_1C.jpg";
});
//mouseout
sImg1.addEventListener("mouseout", function() {
this.src = "images/image_1P.jpg";
});
})