jQuery:缩短添加IE条件的代码?

时间:2013-05-28 10:48:38

标签: jquery internet-explorer-8 conditional

我似乎无法添加IE检测而不重复我的代码并更改单行以允许它与IE 8一起使用... hoverintent是此实例中的罪魁祸首,并且在IE8中不起作用>所以我想检测IE8并简单地使用“悬停”......

我有条件检查器,它可以工作/检测ie8:

if (!$.support.leadingWhitespace) {
alert('This is IE8');
}

这是我的完整代码。例如,在第二行,您有.hoverIntent(...需要更改为MSIE8的简单.hover ...

$(document).ready(function () {
     $('nav li,#mini-menu li').hoverIntent(

function (e) {
  //show its submenu
    e.stopPropagation();
  $('.sub-nav', this).stop(true,true).slideDown(200);
  $('.mini-nav', this).show();
}, 

function (e) {
  //hide its submenu
  e.stopPropagation();
    $('.sub-nav,.mini-nav', this).stop(true,true).fadeOut(100);         
}
 );

我的问题是我在上面添加了条件检查器代码,我想我能做到的唯一方法是复制上面的代码并简单地将它放在条件检查器中并将.hoverintent更改为.hover .. ..所以在我这样做之前,我想问一下是否有更好的方法,我如何在现有代码中插入条件并简单地改变行?

1 个答案:

答案 0 :(得分:1)

你可以这样做:

var isIE = $.support.leadingWhitespace?false:true;

//then

$('nav li,#mini-menu li')[!isIE?'hoverIntent':'hover'](...);