鼠标悬停在图像的特定部分上的图像更改

时间:2016-02-22 15:06:28

标签: javascript html css web frontend

<img src="C:/Users/ma/Desktop/start_new_coin/rebulid from  scratch/chaptermap/ch01ch01.png"width="453" height="453" alt="Planets" usemap="#planetmap">
<map name="planetmap">
<area shape="poly" coords="172,227,181,224,183,213,189,201,195,193,199,190,199,184,126,93,135,93,123,79,103,96,84,117,68,140,58,162,51,186,48,201,47,214,135,219,130,227,148,227,147,225,151,227,149,223,153,227,153,221,156,225,157,214,160,202,166,190,174,179,176,177,174,174,191,176,192,192,189,189,180,200,174,212,172,224,172,227" alt="Sun" onmouseover="this.src='C:/Users/ma/Desktop/start_new_coin/rebulid from scratch/chaptermap/ch01ch12.png'" width="453" height="453" onmouseout="this.src='C:/Users/ma/Desktop/start_new_coin/rebulid from scratch/chaptermap/ch01ch01.png'" width="453" height="453"/>
</map>

当鼠标悬停在特定图像区域时,我正在尝试更改图像。但它没有改变。这是我的代码。我试过这个,但它没有改变图像悬停。

2 个答案:

答案 0 :(得分:0)

您可以使用图像映射。 例如:

<img src="path/to/picture.gif" width="374" height="162" usemap="#map1" id="image" />
<map name="panneaux">
    <area shape="rect" coords="92,19,261,66" onmouseover="action1()" alt="john" />
</map>

Et le js:

action1() {
    document.getElementbyId("image").src = "new image";
}

答案 1 :(得分:0)

您指的是使用this的图片,但this实际上是指触发鼠标事件的元素<area>; <area>代码没有src属性,您可以使用javascript设置该属性。

您需要以不同方式引用图像,例如使用类或ID:

<img src="image1.png" id="image1" width="453" height="453" alt="Planets" usemap="#planetmap">
<map name="planetmap">
    <area shape="poly" coords="..." alt="Sun"
         onmouseover="document.getElementById('image1').src='image2.png'" 
         onmouseout="document.getElementById('image1').src='image1.png'"/>
</map>

(并放弃width标记上的height<area>属性