我在 php 中有一个链接,并使用 javascript 尝试在鼠标悬停时将背景图像从“read.png”制作为“read.png_light”。知道如何让它发挥作用吗?
<?php
echo "<a href='messages.php?hash=$hash'>";
?>
<onmouseover="document.getElementById('mage').src='img/read_light.png';"
onmouseout="document.getElementById('mage').src='img/read.png';">
<img src="img/read.png" id="mage"/>
<?php
echo"</a>";
?>
最佳答案
Onmouseover 不是一个标签,而是一个事件属性! Here are some info
因此,它必须放在元素标签内。
- 在您的问题提供的示例中,onmouseover/onmouseout 是
写得就像它们是标签一样:
<onmouseover>
。这就是它不起作用的原因。 - 在您的第一条评论中提供的示例中,onmouseover 位于内部
anchor 标记,例如
<a onmouseover....></a>
。这就是它起作用的原因。
你尝试过吗:
//onmouseover and onmouseout are inside the <img> tag
<?php
echo "<a href='messages.php?hash=$hash'>";
?>
<img src="img/read.png" id="mage"
onmouseover="document.getElementById('mage').src='img/read_light.png';"
onmouseout="document.getElementById('mage').src='img/read.png';" />
<?php
echo"</a>";
?>
或者
//onmouseover and onmouseout are inside the <a> tag
<?php
echo "<a href='messages.php?hash=$hash' ";
?>
onmouseover="document.getElementById('mage').src='img/read_light.png';"
onmouseout="document.getElementById('mage').src='img/read.png';"
<?php echo ">"; ?>
<img src="img/read.png" id="mage" />
<?php
echo"</a>";
?>
?
关于php 内的链接的 javascript 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20358331/