HTML与JavaScript-将灰度应用于表格中的图像,然后将鼠标图像返回到彩色版本
我有一个问题,即在JavaScript中使用MouseOver / MouseOut事件以及将灰度应用于表格。问题说我必须首先制作html中的图像网格(表)。然后,我需要将JavaScript添加到HTML中,以便当我通过图像鼠标鼠标时,图像会变成彩色图像,并且当我从图像中鼠标鼠标鼠标时,图像会重新回到灰色图像中。 问题说不允许使用CSS,因此只有使用JavaScript和HTML(如果可能)。。 非常感谢您的帮助,我真的很感激!
这是我下面的一些代码(表图像需要从灰度开始,然后在使用MouseOver事件时应用/删除灰度到目前为止,鼠标效果仅在第一个图像上起作用。
function image_grayscale() {
document.getElementById("image").style.filter = "grayscale(100%)";
}
function remove_grayscale() {
document.getElementById("image").style.filter = "grayscale(0%)";
}
<div class="table">
<table border="3" align=center width="600" height="200">
<tr style="width:1" ;style="height:10%" ; bgcolor="white">
<td onmouseover="remove_grayscale()" onmouseout="image_grayscale()">
<img id="image" src="https://picsum.photos/id/1067/100/100" width="100" height="100" />
</td>
<td onmouseover="remove_grayscale()" onmouseout="image_grayscale()">
<img id="image" style="grayscale" src="https://picsum.photos/id/1067/100/100" width="100" height="100" />
</td>
<td onmouseover="remove_grayscale()" onmouseout="image_grayscale()">
<img id="image" src="https://picsum.photos/id/1067/100/100" width="100" height="100" />
</td>
<td onmouseover="remove_grayscale()" onmouseout="image_grayscale()">
<img id="image" src="https://picsum.photos/id/1067/100/100" width="100" height="100" />
</td>
<td onmouseover="remove_grayscale()" onmouseout="image_grayscale()">
<img id="image" src="https://picsum.photos/id/1067/100/100" width="100" height="100" />
</td>
<td onmouseover="remove_grayscale()" onmouseout="image_grayscale()">
<img id="image" src="https://picsum.photos/id/1067/100/100" width="100" height="100" />
</td>
</tr>
</table>
I had a question about using the mouseover / mouseout event in javascript along with applying grayscale to a table. The question says that I must first making an image grid (table) all gray in html. Then I need to add javascript to the html so that when I mouse over the image, the image turns into a colored image, and when I mouse out from the image, the image reverts back into a gray image. The problem said no CSS is allowed, so only using javascript and html, if possible.
Thank you so much in advance for the help, I really appreciate it!
Here is some of my code below (the table images need to start from grayscale, then apply/remove the grayscale when using the mouseover event. So far the mouseover effect only works on the first image. And I also don't know how to apply a grayscale filter over the whole table first).
function image_grayscale() {
document.getElementById("image").style.filter = "grayscale(100%)";
}
function remove_grayscale() {
document.getElementById("image").style.filter = "grayscale(0%)";
}
<div class="table">
<table border="3" align=center width="600" height="200">
<tr style="width:1" ;style="height:10%" ; bgcolor="white">
<td onmouseover="remove_grayscale()" onmouseout="image_grayscale()">
<img id="image" src="https://picsum.photos/id/1067/100/100" width="100" height="100" />
</td>
<td onmouseover="remove_grayscale()" onmouseout="image_grayscale()">
<img id="image" style="grayscale" src="https://picsum.photos/id/1067/100/100" width="100" height="100" />
</td>
<td onmouseover="remove_grayscale()" onmouseout="image_grayscale()">
<img id="image" src="https://picsum.photos/id/1067/100/100" width="100" height="100" />
</td>
<td onmouseover="remove_grayscale()" onmouseout="image_grayscale()">
<img id="image" src="https://picsum.photos/id/1067/100/100" width="100" height="100" />
</td>
<td onmouseover="remove_grayscale()" onmouseout="image_grayscale()">
<img id="image" src="https://picsum.photos/id/1067/100/100" width="100" height="100" />
</td>
<td onmouseover="remove_grayscale()" onmouseout="image_grayscale()">
<img id="image" src="https://picsum.photos/id/1067/100/100" width="100" height="100" />
</td>
</tr>
</table>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ID
属性必须是唯一的。onMouseover
。id
attribute must be unique.onmouseover
.我个人建议以下内容:
参考:
。cssStyledEclaration
eventtarget.addevent.addeventlistlistlistlistener() a>。
nodelist.prototote.prototype.foreach(foreach()
。
I'd personally suggest the following:
References:
CSSStyleDeclaration
.EventTarget.addEventListener()
.NodeList.prototype.forEach()
.