javascript - JS : Change image name on hover

标签 javascript jquery css

我试图在鼠标悬停时将“_hover.jpg”添加到我的图像中,但我很挣扎!非常感谢任何帮助。

<script type="text/javascript">
$(document).ready(function () {


$(".img").mouseover(function (e) {    
$(this).attr("src", $(this).attr("src").replace(".jpg", "_hover.jpg"));
}).mouseout(function (e) {
$(this).attr("src", $(this).attr("src").replace("._hover.jpg", ".jpg"));
});

)} 

<li class="hex">
<div class="hexIn">
<img src="/images/leaderboard.jpg" alt="" class="hex-img"/>
  <h2>Joe Bloggs<br>from ACME<br>volunteered at<br>Inspire<br>on 3 July</h2>
</div></li>

非常感谢

最佳答案

$(".hex-img").mouseover(function () {    
$(this).attr("src", $(this).attr("src").replace(".jpg", "_hover.jpg"));
$(".test").text($(this).attr("src"));
}).mouseout(function () {
$(this).attr("src", $(this).attr("src").replace("_hover.jpg", ".jpg"));
$(".test").text($(this).attr("src"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<p class="test">Hello</p>
<li class="hex">
<div class="hexIn">
<img src="abc.jpg" class="hex-img" width="100px" height="100px"/>
  <h2>Joe Bloggs<br>from ACME<br>volunteered at<br>Inspire<br>on 3 July</h2>
</div></li>

关于javascript - JS : Change image name on hover,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44457097/

相关文章:

javascript - React Navigation 一次呈现所有选项卡

javascript - AngularJS,附加到 $scope 的对象不会绑定(bind)到 html 元素

jquery - 如何在 Apache Wicket 应用程序中向 jQuery TreeTable 添加和展开全部/折叠全部?

javascript - AngularJS 自动功能

javascript - jQuery 切换仅适用于第一个 div

javascript - 使用按钮滚动 slider

Jquery 没有功能

jquery - 在动画 wowslider 分层上覆盖/登录窗口

html - 如何仅在纵向 View 中确定小屏幕的图像宽度?

html - 如何将开关切换按钮添加到 Rails 中的简单表单