当javascript事件的大小通过css设置时,我如何在其上放大图像

How can I make an image enlarge on a javascript event when its size is set through css?

本文关键字:图像 放大 设置 事件 javascript css      更新时间:2023-09-26

我有一些CSS(下面)定义图像大小的

#latestImages img 
{
    height: 95px;
    width: auto;
}  

这会影响这些图像:

<img src="@images.ImagePath" alt="@images.NameOfImage" />

当我将onmouseover事件设置为这样的图像时:

<img src="@images.ImagePath" alt="@images.NameOfImage" onmouseover="this.width=100;this.height=100;" onmouseout="this.width=200;this.height=200;"/>

当查看源时,html中的图像高度和宽度确实发生了变化,但没有可见的变化,但当我删除css时,确实发生了更改。有人知道为什么会这样吗?我能做些什么不同的事情来保持css的原样并让javascript放大图像吗?提前感谢!

您可以在onmouseover处理程序this.style.width="50px"中使用此格式。或者更好的是,不要把JS放在HTML中,而是为你想要做的事情编写一个函数

onmouseover="this.style.width='100px';this.style.height='100px';"
onmouseout="this.style.width='200px';this.style.height='200px';"

样式需要单位。在这种情况下,px。否则它可以是ptinem