2015-04-04 30 views
0

如何停止在IE8/9中運行以下腳本?出於某種原因,有時候這些部分會消失,有時候不會,所以我更喜歡在IE8/9中關閉它。在IE中停止JS

$(document).ready(function() { 

    /* Every time the window is scrolled ... */ 
    $(window).scroll(function(){ 

     /* Check the location of each desired element */ 
     $('.hideme').each(function(i){ 

      var bottom_of_object = $(this).position().top + $(this).outerHeight(); 
      var bottom_of_window = $(window).scrollTop() + $(window).height(); 

      /* If the object is completely visible in the window, fade it it */ 
      if(bottom_of_window > bottom_of_object){ 

       $(this).animate({'opacity':'1'},1500); 

      } 

     }); 

    }); 

}); 
+1

使用'<! - [if gt IE 9]>' – 2015-04-04 16:02:09

+0

唯一的問題是,這是在一個單獨的JS文件,我不認爲你可以把腳本標記內的JS文件?雖然我不確定。 – 2015-04-05 17:04:59

+0

您可以在條件註釋中鏈接該腳本... – Teemu 2015-04-05 18:22:09

回答

0

您可以使用此功能

function IEVersion() { 
    var userAgent = navigator.userAgent.toLowerCase(); 
    var msie = userAgent.indexOf("msie "); 

    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer, return version number 
      return parseFloat((userAgent.match(/.*(?:rv|ie)[\/: ](.+?)([ \);]|$)/) || [])[1]); 

    return false; 
} 

檢測MS IE版本,您可能會發現細節上低於微軟支持網站:

http://support.microsoft.com/en-us/kb/167820

,你必須使用它與你的代碼如下

function IEVersion() { 
    var userAgent = navigator.userAgent.toLowerCase(); 
    var msie = userAgent.indexOf("msie "); 

    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer, return version number 
      return parseFloat((userAgent.match(/.*(?:rv|ie)[\/: ](.+?)([ \);]|$)/) || [])[1]); 

    return false; 
} 

$(document).ready(function() { 

    /* Every time the window is scrolled ... */ 
    var ie = IEVersion(); 
    if(ie == false or (ie != 8 and ie != 9)) 
     your_code(); 

    function your_code() { 
    $(window).scroll(function(){ 

     /* Check the location of each desired element */ 
     $('.hideme').each(function(i){ 

      var bottom_of_object = $(this).position().top + $(this).outerHeight(); 
      var bottom_of_window = $(window).scrollTop() + $(window).height(); 

      /* If the object is completely visible in the window, fade it it */ 
      if(bottom_of_window > bottom_of_object){ 

       $(this).animate({'opacity':'1'},1500); 

      } 

     }); 

    }); 
    } 

}); 
+0

謝謝你,但是,我將如何將結果編碼到我現有的功能?正如我想我需要得到上述功能的結果,所以我原來的腳本只運行,如果它不是IE瀏覽器。 – 2015-04-05 17:06:09