简体   繁体   English

jQuery在悬停时更新图像src

[英]jQuery update image src on hover

I don't want the images to load until the link is hovered, so for that here is the script but it's not working, can someone help me in correcting it? 在链接悬停之前,我不希望加载图像,因此这里是脚本,但是它不起作用,有人可以帮助我进行纠正吗? I added an event listener to each link, and on mouseover called a function that sets the image. 我向每个链接添加了一个事件侦听器,并在鼠标悬停时调用了一个设置图像的函数。 To make things more interesting, we can copy lazyload a bit here and also use the data-original property :) 为了使事情变得更有趣,我们可以在此处复制lazyload并使用data-original属性:)

For example: this is the HTML Code: 例如:这是HTML代码:

<li>
   <a href="#" class="tip_trigger">
      <img class="tip" alt="300 Grams"
              data-original="https://lh5.googleusom/-qCRpw.../300%2520.jpg"
              src='empty.gif' width="90" height="90"/>
      Alex
   </a>
</li>

Then, add an event listener: 然后,添加一个事件侦听器:

Code: 码:

// call for each element with the class 'tip' that's being hovered upon
$('.tip').hover(function()
{
   // retrieve the image inside this element
   var elem = $(this).find('img');

   // set the 'src' to the 'data-original' value
   elem.attr('src', elem.attr('data-original'));
});

for a live demo, you can see this page http://bloghutsbeta.blogspot.com/2012/04/testing-slidernav.html the above part of script is only added to the first letter ALEX in 'A' Category. 对于实时演示,您可以看到此页面http://bloghutsbeta.blogspot.com/2012/04/testing-slidernav.html脚本的以上部分仅添加到“ A”类别中的第一个字母ALEX。

.tip is already the image so you cannot find an image with $(this).find('img'); .tip已经是图像,因此您无法使用$(this).find('img');查找图像$(this).find('img');

try instead 试一试

$('.tip_trigger').hover(function()
{
   // retrieve the image inside this element
   var elem = $(this).find('img');

   // set the 'src' to the 'data-original' value
   elem.attr('src', elem.data('original'));
});

use also .data() method instead of .attr() 也使用.data()方法代替.attr()

var elem = $(this).find('img'); <-- you are looking for an image when this is the image. <-您正在寻找的是图像。

Either replace the element you attach hover to 替换您将鼠标悬停到的元素

$('.tip_trigger').hover(function()
{
   // retrieve the image inside this element
   var elem = $(this).find('img');

   // set the 'src' to the 'data-original' value
   elem.attr('src', elem.attr('data-original'));
});

or keep it the way it is and do not look for an image 或保持原样并且不查找图像

$('.tip').hover(function()
{
   var elem = $(this);

   // set the 'src' to the 'data-original' value
   elem.attr('src', elem.attr('data-original'));
});

You have bound the hover to the img element, therefor you can't find a img child. 您已将鼠标悬停在img元素上,因此找不到img子元素。 Just use this, or change the hover to .tip_trigger. 只需使用它,或将鼠标悬停更改为.tip_trigger。

$('.tip_trigger').hover(function() // That should do the trick
{
   // retrieve the image inside this element
   var elem = $(this).find('img');

   // set the 'src' to the 'data-original' value
   elem.attr('src', elem.attr('data-original'));
});

jQuery.find() searches children. jQuery.find()搜索子级。 You have already found the img with your ".tip"-selector. 您已经通过“ .tip”选择器找到了img。

var elem = $(this);

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

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