2012-10-11 2 views
0

제가 작업하고있는 사이트에 검색 페이지 기능을 넣어야합니다. 온라인에서 하나를 찾았지만 Firefox와 Chrome에서는 훌륭하게 작동하지만 IE에서는 전혀 작동하지 않습니다. 나는이 코드를 작성하지 않았기 때문에 디버깅하기가 특히 어렵다고 생각한다. 어떤 도움이나 지침도 환영합니다!인터넷 익스플로러 검색 페이지 기능

HTML

<form id="f1" name="f1" action="javascript:void(0)" onsubmit="searchpage()" > 
<input id="t1" type="text" name="t1" /> 
<input id="button" type="submit" value="FIND" name="b1" onclick="searchpage()" /> 
</form> 

JAVASCRIPT

function searchpage() { 
    if (document.getElementById("t1").value != null && this.document.getElementById("t1").value != '') parent.findString(document.getElementById("t1").value); 
    return false; 
} 
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) { 
     // 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; 
}​ 

파이어 폭스와 크롬의에서 작동하는 동안주의하는 것이 중요하다 "문자열을 찾을 수 없습니다!" 알림 상자가 작동하지 않습니다.

답변

1

여기에 another answer of mine의 버전이 있습니다.

데모 : http://jsfiddle.net/MRp2G/5/

코드 :

function doSearch(text) { 
    var sel; 
    if (window.find && window.getSelection) { 
     sel = window.getSelection(); 
     if (sel.rangeCount > 0) { 
      sel.collapseToEnd(); 
     } 
     window.find(text); 
    } else if (document.selection && document.body.createTextRange) { 
     sel = document.selection; 
     var textRange; 
     if (sel.type == "Text") { 
      textRange = sel.createRange(); 
      textRange.collapse(false); 
     } else { 
      textRange = document.body.createTextRange(); 
      textRange.collapse(true); 
     } 
     if (textRange.findText(text)) { 
      textRange.select(); 
     } 
    } 
} 
+0

와우! 정말 고맙습니다! 내 모든 수색에서 나는 그것을 찾을 수 없었다! 도와 줘서 고마워! – daniella

+0

그래서 테이블의 특정 카테고리를 검색 할 수 있도록 옵션 목록을 드롭 다운하려고합니다. 모든 열을 ID별로 정렬했습니다. 이 일을 어떻게 할 것인가? – daniella

+0

@ user1634292 : 어떤 비트가 당신을 트립합니까? –