Home > Web Front-end > JS Tutorial > How to implement web page character positioning in javascript_javascript skills

How to implement web page character positioning in javascript_javascript skills

WBOY
Release: 2016-05-16 15:50:23
Original
1460 people have browsed it

The example in this article describes the method of javascript realizing web page character positioning. Share it with everyone for your reference. The details are as follows:

Here you enter the string to be queried, and after clicking the position, the program will automatically jump to the matching position

<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>

Copy after login

I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template