Home  >  Article  >  Web Front-end  >  javascript实现网页字符定位的方法_javascript技巧

javascript实现网页字符定位的方法_javascript技巧

WBOY
WBOYOriginal
2016-05-16 15:50:231389browse

本文实例讲述了javascript实现网页字符定位的方法。分享给大家供大家参考。具体如下:

这里实现输入要查询的字符串,点击定位后程序会自动跳到匹配的位置上

<html>
<form name="f1" action=""
onSubmit="if(this.t1.value!=null && this.t1.value!='')
findString(this.t1.value);return false">
<input type="text" name=t1 value="" size=20>
<input type="submit" name=b1 value="Find">
</form>
<p>
导读:11月2日,美国将举行中期选举,众议院国会议员将全部改选,参议院改选1/3,一些州也将进行州长选举。奥巴马能否令心怀不满的选民支持民主党,支撑民主党统治,尚不得而知
</p>
<script 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 && self.getSelection && !self.getSelection().anchorNode) {
  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;
}
//-->
</script>
</html>

希望本文所述对大家的javascript程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn