【问题标题】:jquery optimising hover event with div and an imagejquery使用div和图像优化悬停事件
【发布时间】:2011-04-16 07:59:36
【问题描述】:

代码:

    $('.featured').hover(
    function() {
        $(this).addClass('active');
        $('img',this).fadeTo('slow', 0.5, function() {})
    },
    function() {
        $(this).removeClass('active');
        $('img',this).fadeTo('slow', 1, function() {})
    });

我该如何改进呢?

我记得曾经有人告诉我不要使用

$('img', this) ..

但我不知道如何访问悬停在任何其他方式上的 DIV 中的图像。

谢谢!

【问题讨论】:

    标签: jquery image html hover


    【解决方案1】:

    你可以使用.find(),像这样:

    $('.featured').hover(function(event) {
        $(this).addClass('active').find('img').fadeTo('slow', 0.5);
    }, function() {
        $(this).removeClass('active').find('img').fadeTo('slow', 1);
    });
    

    这会在您悬停的元素中找到任何<img> 元素...跳过$(selector, context) 必须采取的几个步骤才能确定它真的$(context).find(selector) 调用。也不需要动画回调......它们是可选的,所以如果你在其中做任何事情,就不要使用它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-24
      • 2015-10-03
      • 2014-06-04
      • 2011-03-18
      • 1970-01-01
      • 2011-03-02
      相关资源
      最近更新 更多