简体   繁体   English

使用JavaScript在悬停时更改img src

[英]Change img src on hover with javascript

My code is in PHP and I would want to change the image button1.png to button2.png on hover. 我的代码在PHP中,我想将鼠标悬停时将图像button1.png更改为button2.png。 The image should still be clickable. 该图像仍应可单击。

$link = "<a class='wplink' href='something' title='something' rel='nofollow'>". "<img src='button1.png' />" ."</a>";

Try this, I think it will do. 试试这个,我想会的。

The onmouseout and onmouseover should be on the hyperlink tag. onmouseout和onmouseover应该在超链接标记上。

$link = "<a class='wpfp-link' href='?wpfpaction=".$action."&amp;postid=". $post_id . "' title='". $opt ."' rel='nofollow' onmouseover=\"document.but.src='".WPFP_PATH."/vault-button2.png'\" onmouseout=\"document.but.src='".WPFP_PATH."/vault-button1.png'\">

"; “;

试试这个:

echo $link = "<a class='wpfp-link' href='?wpfpaction=".$action."&postid=". $post_id . "' title='". $opt ."' rel='nofollow' onmouseover='javascript:document.getElementById(\"but\").src=\"".WPFP_PATH."/vault-button2.png\"' onmouseout='javascript:document.getElementById(\"but\").src=\"".WPFP_PATH."/vault-button1.png\"' > <img src='".WPFP_PATH."/vault-button1.png' name='but' id='but'/></a>";

Try this: 尝试这个:

$("img").on("mouseover", function(){
    $(this).attr("src","img2.png");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM