我有6个图像,我希望改变他们旁边的文本,基于哪个图像是用鼠标“悬停”。使用onmouseover和onmouseoff与图像来改变文字
我尝试使用JavaScript与onmouseover和onmouseout事件,使用一些代码直接复制工作示例,但我的代码目前不工作。
这里是一个的jsfiddle:https://jsfiddle.net/schanz/xh4pb9n6/
此外,这里是原始代码:`
<h3>Cruise Destinations</h3>
<table>
<tr>
<td>
<img src="http://i.imgur.com/tPJGobK.jpg?1" onmouseover="onHover(0)" onmouseout="offHover()">
</td>
<td>
<img src="http://i.imgur.com/syWPecu.png" onmouseover="onHover(1)" onmouseout="offHover()">
</td>
<td>
<img src="http://i.imgur.com/sESpwWx.jpg?1" onmouseover="onHover(2)" onmouseout="offHover()">
</td>
<td colspan="2" >
<p id='textField'>Mouse over an image to learn about that destination.</p>
</td>
</tr>
<td>
<img src="http://i.imgur.com/zFnhhv2.jpg?1" onmouseover="onHover(3)" onmouseout="offHover()">
</td>
<td>
<img src="http://i.imgur.com/WAYyBmv.jpg?1" onmouseover="onHover(4)" onmouseout="offHover()">
</td>
<td>
<img src="http://i.imgur.com/ZbwvBDE.jpg?1" onmouseover="onHover(5)" onmouseout="offHover()" >
</td>
<tr>
</tr>
</table>
<script>
var textFields = ["Stellwagen Bank offers thrilling whale-watching opportunities.",
"Hyannis is filled with great food and fun clubs in a downtown setting.",
"Falmouth is perfect for beach-lovers.",
"Oak Bluffs feature some of Massachusetts best cottages.",
"Nantucket is one of our favorites and we think you will love it too.",
"Plymouth rock is a must for history buffs."];
function onHover(num) {
document.getElementById('textField').innerhtml = textFields[num];
}
function offHover() {
document.getElementById('textField').innerhtml =
"Mouse over an image to learn about that destination.";
}
</script>
`
变化innerHTML来'innerHTML'在您的脚本。这将完成这项工作。 – Ofisora
谢谢Ofisora,那就是问题所在。 – Schanz97