【发布时间】:2017-02-07 16:11:39
【问题描述】:
我想在将鼠标悬停在图像上时更改图像的来源。
我已经设法为一张图片做到了,通过
<img id="image1" src="image1-grey.png" onmouseover=colorImage(this) />"
还有:
function colorImage(x){
document.getElementById("image1").src = x.src.replace("grey", "color");
}
我有一打左右的图片 - 都有两个版本,灰色和彩色。
现在我想我可以对所有图像单独重复上述功能,但必须有一种更简洁的方法。
这是我想的,但它不起作用:
<img id="image1" src="image1-grey.png" onmouseover=colorImage(image1) />"
还有:
function colorImage(x){
document.getElementById(x).src = this.src.replace("grey", "color");
}
我想,这样一来,所有图像都只有一个功能。但不是。为什么不呢?
提前非常感谢!
【问题讨论】:
标签: javascript replace image