2014-01-13 65 views
1

工作,我使用從腳本:http://www.javascripter.net/faq/searchin.htm 但這不是InternetExplorer的11搜索文本腳本不IE11

工作了任何想法,以解決這一問題?並使其適用於所有瀏覽器?

var TRange=null 

function findString (str, win) { 
    if (parseInt(navigator.appVersion)<4) return; 
    var strFound; 
    if (navigator.appName=="Netscape") { 

     if (!win) win = self; 
     win.focus(); 

     // NAVIGATOR-SPECIFIC CODE 

     strFound=win.find(str); 
     if (!strFound) { 
      strFound=win.find(str,0,1) 
      while (win.find(str,0,1)) continue 
     } 
    } 
    if (navigator.appName.indexOf("Microsoft")!=-1) { 

    // EXPLORER-SPECIFIC CODE 

    if (TRange!=null) { 
     TRange.collapse(false) 
     strFound=TRange.findText(str) 
     if (strFound) TRange.select() 
    } 
    if (TRange==null || strFound==0) { 
     TRange=win.document.body.createTextRange() 
     strFound=TRange.findText(str) 
     if (strFound) TRange.select() 
    } 
} 
if (!strFound) alert ("Helaas '"+str+"' werd niet gevonden! Probeer iets anders...") 
} 
+0

我的IE 11的謊言和說,這是網景,所以我改變'== 「Netscape的」'來使用特徵檢測如'&& typeof(win?win:self).find!='undefined'' –

回答

0

如果問題只在IE11的情況下,所以考慮下面的代碼

<head> 
<title></title> 
<script type="text/javascript" language="javascript" > 
    var TRange = null; 
    function findString(str) { 
     if (parseInt(navigator.appVersion) < 4) return; 
     var strFound; 
     if (window.find) { 
      // CODE FOR BROWSERS THAT SUPPORT window.find 
      strFound = self.find(str); 
      if (!strFound) { 
       strFound = self.find(str, 0, 1); 
       while (self.find(str, 0, 1)) continue; 
      } 
     } 
     else if (navigator.appName.indexOf("Microsoft") != -1 || navigator.appName.toString().toUpperCase()== "NETSCAPE") { 
      // EXPLORER-SPECIFIC CODE 
      if (TRange != null) { 
       TRange.collapse(false); 
       strFound = TRange.findText(str); 
       if (strFound) TRange.select(); 
      } 
      if (TRange == null || strFound == 0) { 
       TRange = self.document.body.createTextRange(); 
       strFound = TRange.findText(str); 
       if (strFound) TRange.select(); 
      } 
     } 
     else if (navigator.appName == "Opera") { 
      alert("Opera browsers not supported, sorry...") 
      return; 
     } 
     if (!strFound) alert("String '" + str + "' not found!") 
     return; 
    } 
</script> 
</head> 
<body> 
<input type="button" id="btnchk" onclick="findString('abc');" /> 
    abc 
    <span> 
    </span> 
    pkr 
</body> 
</html>