【问题标题】:jQuery hovers don't work in IE10 but works in lower IE versionsjQuery 悬停在 IE10 中不起作用,但在较低的 IE 版本中起作用
【发布时间】:2013-04-06 23:03:43
【问题描述】:

在我的网站上,这些框应该淡入描述中。这适用于我尝试过的所有浏览器,除了 IE10。这是一个 WordPress 网站,我使用了九个插件。我已经尝试停用所有插件,但悬停在 IE10 中仍然不起作用。

以下是我用于悬停的代码:

jQuery(document).ready(function($){

$('.thumbnail').hover(function() {
  $('img', this).stop(true,true).fadeTo(100, 0.1);
  $('.description', this).stop(true,true).fadeIn(100);
}, function() {
  $('img', this).stop(true,true).fadeTo(100, 1);
  $('.description', this).stop(true,true).fadeOut(100);
});

});

有人可以帮我找出问题吗?如果我遗漏了任何重要信息,请告诉我。谢谢。

编辑:过滤/悬停的整个代码。

  // Filter
  $(function() {

    var time_effect = 1000;
    var effect_name = 'easeOutQuart';

    $('.all').quicksand( $('.everything article'), {
        duration: time_effect,
        attribute: 'data-id',
        easing: effect_name,
        adjustHeight: 'auto',
        useScaling: false
      }, function() { // callback function
            $("a.single-image").fancybox({
                'transitionIn'   : 'elastic',
                'transitionOut'  : 'fade',
                'overlayColor'   : '#000',
                'overlayOpacity' : '0.6'
            });
            $(document.body)
                .on('mouseenter', '.thumbnail', function() {
                    $('img', this).stop(true,true).fadeTo(600, 0);
                    $('.description', this).stop(true,true).fadeIn(600);
                })
                .on('mouseleave', '.thumbnail', function() {
                    $('img', this).stop(true,true).fadeTo(600, 1);
                    $('.description', this).stop(true,true).fadeOut(600);
                });

            $("body.blog #content article:last, body.archive #content article:last, body.search-results #content article:last").addClass("last");
      });

    $('.btn_all').click(function(e) {
      $('.all').quicksand( $('.everything article'), {
        duration: time_effect,
        attribute: 'data-id',
        easing: effect_name,
        adjustHeight: 'auto',
        useScaling: false
      }, function() { // callback function
            $("a.single-image").fancybox({
                'transitionIn'   : 'elastic',
                'transitionOut'  : 'fade',
                'overlayColor'   : '#000',
                'overlayOpacity' : '0.6'
            });
            $(document.body)
                .on('mouseenter', '.thumbnail', function() {
                    $('img', this).stop(true,true).fadeTo(600, 0);
                    $('.description', this).stop(true,true).fadeIn(600);
                })
                .on('mouseleave', '.thumbnail', function() {
                    $('img', this).stop(true,true).fadeTo(600, 1);
                    $('.description', this).stop(true,true).fadeOut(600);
                });
            $("body.blog #content article:last, body.archive #content article:last, body.search-results #content article:last").addClass("last");
      });
      $('.button_box a').removeClass('selected');
      $(this).addClass('selected');
      e.preventDefault();
    });

    $('.btn_identity').click(function(e) {
      $('.all').quicksand( $('.identity article'), {
        duration: time_effect,
        attribute: 'data-id',
        easing: effect_name,
        adjustHeight: 'auto',
        useScaling: false
      }, function() { // callback function
            $("a.single-image").fancybox({
                'transitionIn'   : 'elastic',
                'transitionOut'  : 'fade',
                'overlayColor'   : '#000',
                'overlayOpacity' : '0.6'
            });
            $(document.body)
                .on('mouseenter', '.thumbnail', function() {
                    $('img', this).stop(true,true).fadeTo(600, 0);
                    $('.description', this).stop(true,true).fadeIn(600);
                })
                .on('mouseleave', '.thumbnail', function() {
                    $('img', this).stop(true,true).fadeTo(600, 1);
                    $('.description', this).stop(true,true).fadeOut(600);
                });
            $("body.blog #content article:last, body.archive #content article:last, body.search-results #content article:last").addClass("last");
      });
      $('.button_box a').removeClass('selected');
      $(this).addClass('selected');
      e.preventDefault();
    });

    $('.btn_web').click(function(e) {
      $('.all').quicksand( $('.web article'), {
        duration: time_effect,
        attribute: 'data-id',
        easing: effect_name,
        adjustHeight: 'auto',
        useScaling: false
      }, function() { // callback function
            $("a.single-image").fancybox({
                'transitionIn'   : 'elastic',
                'transitionOut'  : 'fade',
                'overlayColor'   : '#000',
                'overlayOpacity' : '0.6'
            });
            $(document.body)
                .on('mouseenter', '.thumbnail', function() {
                    $('img', this).stop(true,true).fadeTo(600, 0);
                    $('.description', this).stop(true,true).fadeIn(600);
                })
                .on('mouseleave', '.thumbnail', function() {
                    $('img', this).stop(true,true).fadeTo(600, 1);
                    $('.description', this).stop(true,true).fadeOut(600);
                });
            $("body.blog #content article:last, body.archive #content article:last, body.search-results #content article:last").addClass("last");
      });
      $('.button_box a').removeClass('selected');
      $(this).addClass('selected');
      e.preventDefault();
    });

    $('.btn_illustration').click(function(e) {
      $('.all').quicksand( $('.illustration article'), {
        duration: time_effect,
        attribute: 'data-id',
        easing: effect_name,
        adjustHeight: 'auto',
        useScaling: false
      }, function() { // callback function
            $("a.single-image").fancybox({
                'transitionIn'   : 'elastic',
                'transitionOut'  : 'fade',
                'overlayColor'   : '#000',
                'overlayOpacity' : '0.6'
            });
            $(document.body)
                .on('mouseenter', '.thumbnail', function() {
                    $('img', this).stop(true,true).fadeTo(600, 0);
                    $('.description', this).stop(true,true).fadeIn(600);
                })
                .on('mouseleave', '.thumbnail', function() {
                    $('img', this).stop(true,true).fadeTo(600, 1);
                    $('.description', this).stop(true,true).fadeOut(600);
                });
            $("body.blog #content article:last, body.archive #content article:last, body.search-results #content article:last").addClass("last");
      });
      $('.button_box a').removeClass('selected');
      $(this).addClass('selected');
      e.preventDefault();
    });

    $('.btn_print').click(function(e) {
      $('.all').quicksand( $('.print article'), {
        duration: time_effect,
        attribute: 'data-id',
        easing: effect_name,
        adjustHeight: 'auto',
        useScaling: false
      }, function() { // callback function
            $("a.single-image").fancybox({
                'transitionIn'   : 'elastic',
                'transitionOut'  : 'fade',
                'overlayColor'   : '#000',
                'overlayOpacity' : '0.6'
            });
            $(document.body)
                .on('mouseenter', '.thumbnail', function() {
                    $('img', this).stop(true,true).fadeTo(600, 0);
                    $('.description', this).stop(true,true).fadeIn(600);
                })
                .on('mouseleave', '.thumbnail', function() {
                    $('img', this).stop(true,true).fadeTo(600, 1);
                    $('.description', this).stop(true,true).fadeOut(600);
                });
            $("body.blog #content article:last, body.archive #content article:last, body.search-results #content article:last").addClass("last");
      });
      $('.button_box a').removeClass('selected');
      $(this).addClass('selected');
      e.preventDefault();
    });
  });

【问题讨论】:

  • 你试过用 mouseenter/mouseleave 代替吗?
  • '.thumbnail'是动态生成的吗?
  • @roasted,不,我没试过mouseenter/mouseleave。 @musa 不,它们不是动态生成的。
  • 看起来你的事件处理程序没有被绑定,如果你从控制台再次运行相同的代码它可以工作。
  • @Musa 是正确的 - 从控制台运行相同的代码可以修复它,这一定意味着当该代码首次运行时,.thumbnail 对象尚未出现在页面上。如果您无法调整该代码首次运行的时间,则使用带有选择器的 jQuery.on 将处理页面上尚不存在的元素。

标签: jquery internet-explorer internet-explorer-10 jquery-hover quicksand


【解决方案1】:

这里的问题在于浏览器嗅探代码;将 Internet Explorer 10 的用户代理字符串* 更改为 Chrome 的,然后刷新浏览器。你会发现它工作得很好。

脚本利用$.browser.msie 并假设 Internet Explorer 10 与 Internet Explorer 7、8 和 9 类似(事实并非如此,它完全不同)。您的情况的罪魁祸首来自 jQuery QuickSand。看看以下情况:

if ( $.browser.msie && $.browser.version.substr(0,1) < 7 ) {
    $sourceParent.html('').append($collection);
    return;
}

这个测试,只检查版本号的第一个值,会错误地将 Internet Explorer 10 识别为版本“1”,这显然小于支持的版本 7。

已修复six months agodownload the latest version 来自 GitHub 的 QuickSand。

更改 Internet Explorer 的用户代理字符串

按 F12 显示开发者工具。选择 Tools > Change user agent string 以显示其他 ua-string 选项。选择“Chrome”,刷新浏览器,重新以“Chrome”发出HTTP请求。

【讨论】:

    【解决方案2】:

    您可以通过将其插入当前对.hover 的调用上方的行中来了解在运行代码时IE 10 是否存在.thumbnail 对象:

    console.log($('.thumbnail').length);
    

    如果这记录了0,那么这就解释了为什么悬停事件处理程序永远不会连接。无论出于何种原因,IE 10 认为这些元素在代码运行时还不存在。这是绑定事件的另一种方法,可能效果更好:

    jQuery(document).ready(function($){
    
        $(document.body)
            .on('mouseenter', '.thumbnail', function() {
                $('img', this).stop(true,true).fadeTo(100, 0.1);
                $('.description', this).stop(true,true).fadeIn(100);
            })
            .on('mouseleave', '.thumbnail', function() {
                $('img', this).stop(true,true).fadeTo(100, 1);
                $('.description', this).stop(true,true).fadeOut(100);
            });
    
    });
    

    它的作用是设置一对全局事件处理程序,这些事件处理程序仅适用于具有“缩略图”类名称的元素,而不管这些元素在此代码首次运行时是否存在。如果问题是 IE 10 中的时间问题,这可能会解决它。

    【讨论】:

    • 我做了这个更改,但悬停在 IE10 中仍然不起作用。如果有帮助的话,我用过滤/悬停的完整代码更新了我的原始帖子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 2012-12-18
    • 2014-06-10
    • 1970-01-01
    • 2015-08-08
    • 2012-09-16
    相关资源
    最近更新 更多