2004/07/23 | 来自MyIE2插件的页面文字查找
类别(JScript) | 评论(0) | 阅读(119) | 发表于 10:50
<html>
<head>
<title> New Document </title>
<script language="javascript">

function strip(x,c) {
while (x.substring(0,1) == c) x = x.substring(1);
while (x.substring(x.length-1,x.length) == c) x = x.substring(0,x.length-1);
return x;
}

function search() {
var r,i,s;
    s=window.document.selection.createRange().text;
    if(!s) {
     s=prompt('Enter text to highlight:','');
    } else {
     s=strip(s,' ');
     window.document.selection.empty();
    }

    if(s) {
     r=window.document.body.createTextRange();
     for(i=0;r.findText(s);i++) {
     r.execCommand("BackColor","","yellow");
     r.execCommand("ForeColor","","red");
     r.execCommand("Bold","","");
     r.collapse(false);
     }
     if (i==0) {
     alert('\"'+ s +'\"\r\n not found on this page');
     } else {
     if (i==1) sm=' occurrence';
     else sm=' occurrences';

     status='TextHighlighter : '+i+sm+' of \"' + s + '\" highlighted';

     window.clipboardData.setData("Text",s);
     }
    }
}
</script>
</head>

<body>
点击一链接弹出promot,输入文字,点击确定后,如何实现CTRL+F全文查找的功能,如果网页中有promot的文字,就被选中,否则弹出没有的信息ALERT
<button onclick=search()>Search</button>
</body>
</html>
0

评论Comments

日志分类
首页[102]
.Net[11]
VBScript[1]
JScript[51]
XML[3]
HTML&CSS[9]
ASP[8]
ActiveX[4]
Software[10]
Other[5]