javascript - .hover() Jquery 需要帮助

标签 javascript jquery html css

<分区>

我在实现 jquery 功能时遇到问题,该功能允许我将鼠标悬停在图像上并更改图像,然后在不悬停时将其恢复为原始图像。这是我目前所拥有的,任何帮助将不胜感激。

<div id="slideshow">
        <img src="myPic.jpg" width="400" height="300" alt="pic" id="myImage"/>
</div>

$(document).ready(function(){
 $("#myImage").hover(function(){
    $("#myImage").attr("src", "http://timesofoman.com/uploads/images/2017/05/21/671029.JPG");
    },function() {
        $("#myImage").attr("src", "myPic.jpg");
    });
  });
}

最佳答案

如果你想在鼠标离开时改变图像,你必须添加 mouseout 事件。

$("#myImage").hover(function(){
  $(this).attr("src", 
     "http://timesofoman.com/uploads/images/2017/05/21/671029.JPG");
  }).on("mouseout",function(){
  $(this).attr("src", "myPic.jpg");
});

$("#myImage").hover(function(){
      $(this).attr("src", 
         "http://www.thehindu.com/news/national/other-states/article20605531.ece/alternates/LANDSCAPE_460/20TH-THGRP-RUPANI");
      }).on("mouseout",function(){
      $(this).attr("src", "http://www.thehindu.com/news/national/article20601353.ece/alternates/FREE_460/21THCRPF-02");
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<img id="myImage" src="http://www.thehindu.com/news/national/other-states/article20605531.ece/alternates/LANDSCAPE_460/20TH-THGRP-RUPANI" />

关于javascript - .hover() Jquery 需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47405368/

上一篇:html - Chrome 检查工具不反射(reflect)浏览器窗口,对齐 Div

下一篇:html - 背景图片仅在内容位于 div 中时出现

相关文章:

html - 为什么正确打印网页如此困难?

html - Bootstrap 将元素对齐到按钮的左侧

javascript - 如何将 "pointers"存储到 React/Javascript 中一棵不可变树中的嵌套节点?

javascript - 在 Windows Phone 上禁用向右滑动到上一页

jquery - 调整元素大小会触发窗口的调整大小事件

javascript - 如何使用嵌入式 javascript 在第 3 方网站上创建弹出窗口?

html - Bootstrap 4 六个盒子在同一行(响应式)?

javascript - phalcon volt "javascript_include"如何在包含 URL 中添加异步或延迟

javascript - 是否有支持 JavaScript View 的 ASP.Net MVC View 引擎?

javascript - 如何让多个单选按钮在表单上保持选中状态