【问题标题】:jquery change image on mouse rolloverjquery在鼠标悬停时更改图像
【发布时间】:2012-10-24 22:39:09
【问题描述】:

我一直有这个问题。我认为这应该很简单,但我似乎无法让它工作。我希望滚动我的 facebook 按钮时出现一个新图像。感谢您的帮助!

    <p align="right">
          <a href="http://www.facebook.com/AerialistPress" ><img height="30px" id="facebook" class="changePad" alt="Aerialist Press Facebook Page" src="/sites/aerialist.localhost/files/images/facebook300.jpg" /></a> 
           <a href="http://twitter.com/#!/AerialistPress" > <img height="30px" class="changePad" alt="Aerialist Press Twitter Page" src="/sites/aerialist.localhost/files/images/twitter300.jpg" /></a>
           <a href="http://www.pinterest.com/aerialistpress" ><img height="30px" class="changePad" alt="Aerialist Press Pinterest Page" src="/sites/aerialist.localhost/files/images/pinterest300.jpg" /></a>
</p>

<script>
jQuery(document).ready(function(){
     jQuery('#facebook').mouseover(function() { jQuery('#facebook').attr('src').replace('/sites/aerialist.localhost/files/images/facebook-roll.jpg'); })

});
</script>

【问题讨论】:

    标签: jquery html rollover


    【解决方案1】:

    attr 方法返回指定属性的值(在本例中为“src”),而replace 正在尝试修改字符串并返回一个新实例。但是,您没有对新实例做任何事情。

    要设置属性,您必须向attr 方法传递一个附加参数。

    这是attr 方法的文档: http://api.jquery.com/attr/

    你的代码应该是:

    jQuery('#facebook').attr('src', '/sites/aerialist.localhost/files/images/facebook-roll.jpg');
    

    【讨论】:

      【解决方案2】:

      不要使用replace,直接设置src attr即可:

      jQuery('#facebook').attr('src', '/sites/aerialist.localhost/files/images/facebook-roll.jpg');
      

      【讨论】:

      • 谢谢!它发生了变化,但是当我将鼠标移出时,它保持不变。有没有办法让它在鼠标移出时变回?
      【解决方案3】:

      改变

      jQuery('#facebook').attr('src').replace('/sites/aerialist.localhost/files/images/facebook-roll.jpg');
      

      jQuery('#facebook').attr('src', '/sites/aerialist.localhost/files/images/facebook-roll.jpg');
      

      您也可以使用$('#facebook') 代替$('#facebook')

      【讨论】:

      【解决方案4】:

      给你!

      $("#img").hover(function(){
      //mouseover
           $(this).attr('src', 'https://twimg0-a.akamaihd.net/profile_images/1768071248/smile_normal.jpg'); 
          },
          function(){
      //mouseout
          $(this).attr('src', 'http://www.bestfreeicons.com/smimages/Emotes-face-smile-icon.png');
      });​
      

      【讨论】:

        【解决方案5】:

        这行得通

        <script type ="text/javascript">
        
         $(document).ready(function(){
         $('#facebook').mouseenter(function() { 
             $('#facebook').attr("src","heretheotherimage.png"); 
         })
        
         $('#facebook').mouseleave(function() { 
             $('#facebook').attr("src","heretheimage.png"); 
         })
        
         });
         </script>
        
         <img src ="heretheimage.png" id ="facebook" />   
        

        【讨论】:

          猜你喜欢
          • 2018-11-07
          • 1970-01-01
          • 2010-11-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多