===========在JS中查找文本框中的文字并选中========= 本帖最后由 huangwenquan123 于 2012-05-22 09:16:13 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><style>html,body{width:100%;height:100%}</style></head><body><div style="width:400px;height:2000px;background-color:#F00;float:right"><div></div></div><div style="width:400px; height:200px; background-color:#F00"></div><div style="width:400px; height:200px; background-color:#F00;float:right"></div><input type="button"></input><input type="text"></input><textarea id='txtText' cols=50 rows=6>我想页面加载的时候</textarea></body><script>var txt = "时";var txtText = document.getElementById('txtText');var start = txtText.value.replace(/\r\n/ig,'$').indexOf(txt); // IE 换行符 \r\n 需要处理一下var end = start + txt.length;setSelectRange(txtText,start,end); /*** 设置textarea的选中区域*/ function setSelectRange( textarea, start, end ){ if ( typeof textarea.createTextRange != 'undefined' )// IE { var range = textarea.createTextRange(); // 先把相对起点移动到0处 range.moveStart( "character", 0) range.moveEnd( "character", 0); range.collapse( true); // 移动插入光标到start处 range.moveEnd( "character", end); range.moveStart( "character", start); range.select(); } // if else if ( typeof textarea.setSelectionRange != 'undefined' ) { textarea.setSelectionRange(start, end); textarea.focus(); } // else} window.onload=function(e){bindEvent(document.body,'click',function(e){ e=e||event; var obj = e.srcElement || e.target; if(typeof(obj.innerHTML)=='string'){ var div=document.createElement("div"); var x=e.x||e.pageX; var y=e.y||e.pageY; div.style.cssText="position:absolute;left:"+x+"px;top:"+y+"px;"; div.innerHTML="Good"; document.body.appendChild(div); }});};function bindEvent(obj,evt,fun) { if(window.addEventListener){ obj.addEventListener(evt, function(e){ fun(e);},false); }else{ obj.attachEvent('on'+evt,fun); }}var xxx = {a:{a1:"a1"},b:{b1:{b11:1,b12:2}}};var yyy = {b:{b1:{b11:4,b12:5}}};for(var y in yyy){xxx[y] = yyy[y];}function getMatchs(str,tag){var reg =eval('/\\['+tag+'\\](.+?)\\[\\/'+tag+'\\]/i');return str.match(reg);}var m = getMatchs('aaaa[cn]中文[/cn]bbbb[en]英文[/en]ccccc','cn');//alert(m.length +"\r\n" +m[0]+"\r\n"+m[1]);</script></html> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><style>html,body{width:100%;height:100%}</style></head><body><textarea id='txtText' cols=50 rows=6>我想页面加载的时候</textarea></body><script>var txt = "时";var txtText = document.getElementById('txtText');var start = txtText.value.replace(/\r\n/ig,'$').indexOf(txt); // IE 换行符 \r\n 需要处理一下var end = start + txt.length;setSelectRange(txtText,start,end); /*** 设置textarea的选中区域*/ function setSelectRange( textarea, start, end ){ if ( typeof textarea.createTextRange != 'undefined' )// IE { var range = textarea.createTextRange(); // 先把相对起点移动到0处 range.moveStart( "character", 0) range.moveEnd( "character", 0); range.collapse( true); // 移动插入光标到start处 range.moveEnd( "character", end); range.moveStart( "character", start); range.select(); } // if else if ( typeof textarea.setSelectionRange != 'undefined' ) { textarea.setSelectionRange(start, end); textarea.focus(); } // else} </script></html> 帅,原来是ie多了个\r.害我纠结了一早上.我也刚好写好了,原本ie是用findText来处理,ff用selectionStart来处理. var txtBox = document.getElementById("text_Area"); var index=0; if(document.selection) rng = txtBox.createTextRange(); function findText(str){ if(str=="") return; str=str.toLowerCase(); if(document.selection){ var num = document.selection.createRange().text.length; rng.moveStart("character",num); rng.moveEnd("character",txtBox.value.length); if(rng.findText(str)) rng.select(); if(rng.text.toLowerCase()!=str){ alert("已到达文件结尾"); rng = txtBox.createTextRange(); } } else{ index = txtBox.value.toLowerCase().indexOf(str,index); if(index==-1){ alert("已到达文件结尾"); return; } txtBox.selectionStart=index; var end = index+str.length; txtBox.selectionEnd=end; txtBox.focus(); index++; } }最后还是用你那个方法.<body> <textarea id="txtText" cols="120" rows="20"></textarea> <input id="txtSearch" type="text" /> <input id="Button1" type="button" value="搜索" onclick="Search()" /></body><script type="text/javascript">var txtSearch = document.getElementById("txtSearch");var txtText = document.getElementById("txtText");var Start=0,End=0;function Search(){ if(txtSearch.value.length==0){ alert("请输入要搜索的字符!"); return; } Start = txtText.value.toLowerCase().replace(/\r\n/ig,'\n').indexOf(txtSearch.value.toLowerCase(),Start); End = Start+txtSearch.value.length; if(Start==-1){ alert("已到达文件结尾!"); return; } if(document.selection){ var range=txtText.createTextRange(); range.collapse(true); range.moveEnd("character",End); range.moveStart("character",Start); range.select(); } else{ txtText.setSelectionRange(Start, End); txtText.focus(); } Start++;}</script> JavaScript.DOM编程 在aptana 中编写html 不能执行 请教!如下代码是显示带按钮点击左右滚动的图片效果,如何才能改成为上下滚动的呢? 我写了一个EXT 下拉框远程获取数据 ,无法显示,请教各位高手 windows中的IP与子网掩码按照什么算法来确定有效性的 jsp页面之间传递参数及相关 求动态修改树型控件的方法 请问段代码是什么意思?得出的结果是什么? 动态增加单元格的问题 请问怎样将一个text的值传给file控键的值!!急急 js,求正则字母大写 看不懂INF文件,懂得进来帮忙下,谢了
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
html,body{width:100%;height:100%}
</style>
</head>
<body>
<div style="width:400px;height:2000px;background-color:#F00;float:right">
<div></div>
</div>
<div style="width:400px; height:200px; background-color:#F00"></div>
<div style="width:400px; height:200px; background-color:#F00;float:right"></div>
<input type="button"></input>
<input type="text"></input>
<textarea id='txtText' cols=50 rows=6>
我
想
页
面
加
载
的
时
候
</textarea></body>
<script>var txt = "时";
var txtText = document.getElementById('txtText');
var start = txtText.value.replace(/\r\n/ig,'$').indexOf(txt); // IE 换行符 \r\n 需要处理一下
var end = start + txt.length;
setSelectRange(txtText,start,end); /**
* 设置textarea的选中区域
*/
function setSelectRange( textarea, start, end )
{
if ( typeof textarea.createTextRange != 'undefined' )// IE
{
var range = textarea.createTextRange();
// 先把相对起点移动到0处
range.moveStart( "character", 0)
range.moveEnd( "character", 0);
range.collapse( true); // 移动插入光标到start处
range.moveEnd( "character", end);
range.moveStart( "character", start);
range.select();
} // if
else if ( typeof textarea.setSelectionRange != 'undefined' )
{
textarea.setSelectionRange(start, end);
textarea.focus();
} // else
}
window.onload=function(e){bindEvent(document.body,'click',function(e){
e=e||event;
var obj = e.srcElement || e.target;
if(typeof(obj.innerHTML)=='string'){
var div=document.createElement("div");
var x=e.x||e.pageX;
var y=e.y||e.pageY;
div.style.cssText="position:absolute;left:"+x+"px;top:"+y+"px;";
div.innerHTML="Good";
document.body.appendChild(div);
}
});
};function bindEvent(obj,evt,fun)
{
if(window.addEventListener){
obj.addEventListener(evt, function(e){ fun(e);},false);
}else{
obj.attachEvent('on'+evt,fun);
}
}
var xxx = {a:{a1:"a1"},b:{b1:{b11:1,b12:2}}};
var yyy = {b:{b1:{b11:4,b12:5}}};
for(var y in yyy)
{
xxx[y] = yyy[y];
}
function getMatchs(str,tag){
var reg =eval('/\\['+tag+'\\](.+?)\\[\\/'+tag+'\\]/i');
return str.match(reg);
}
var m = getMatchs('aaaa[cn]中文[/cn]bbbb[en]英文[/en]ccccc','cn');
//alert(m.length +"\r\n" +m[0]+"\r\n"+m[1]);</script>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
html,body{width:100%;height:100%}
</style>
</head>
<body>
<textarea id='txtText' cols=50 rows=6>
我
想
页
面
加
载
的
时
候
</textarea></body>
<script>var txt = "时";
var txtText = document.getElementById('txtText');
var start = txtText.value.replace(/\r\n/ig,'$').indexOf(txt); // IE 换行符 \r\n 需要处理一下
var end = start + txt.length;
setSelectRange(txtText,start,end); /**
* 设置textarea的选中区域
*/
function setSelectRange( textarea, start, end )
{
if ( typeof textarea.createTextRange != 'undefined' )// IE
{
var range = textarea.createTextRange();
// 先把相对起点移动到0处
range.moveStart( "character", 0)
range.moveEnd( "character", 0);
range.collapse( true); // 移动插入光标到start处
range.moveEnd( "character", end);
range.moveStart( "character", start);
range.select();
} // if
else if ( typeof textarea.setSelectionRange != 'undefined' )
{
textarea.setSelectionRange(start, end);
textarea.focus();
} // else
}
</script>
</html>
我也刚好写好了,原本ie是用findText来处理,ff用selectionStart来处理. var txtBox = document.getElementById("text_Area");
var index=0;
if(document.selection)
rng = txtBox.createTextRange();
function findText(str){
if(str=="")
return;
str=str.toLowerCase();
if(document.selection){
var num = document.selection.createRange().text.length;
rng.moveStart("character",num);
rng.moveEnd("character",txtBox.value.length);
if(rng.findText(str))
rng.select();
if(rng.text.toLowerCase()!=str){
alert("已到达文件结尾");
rng = txtBox.createTextRange();
}
}
else{
index = txtBox.value.toLowerCase().indexOf(str,index);
if(index==-1){
alert("已到达文件结尾");
return;
}
txtBox.selectionStart=index;
var end = index+str.length;
txtBox.selectionEnd=end;
txtBox.focus();
index++;
}
}
最后还是用你那个方法.<body>
<textarea id="txtText" cols="120" rows="20"></textarea>
<input id="txtSearch" type="text" />
<input id="Button1" type="button" value="搜索" onclick="Search()" />
</body>
<script type="text/javascript">
var txtSearch = document.getElementById("txtSearch");
var txtText = document.getElementById("txtText");
var Start=0,End=0;
function Search(){
if(txtSearch.value.length==0){
alert("请输入要搜索的字符!");
return;
}
Start = txtText.value.toLowerCase().replace(/\r\n/ig,'\n').indexOf(txtSearch.value.toLowerCase(),Start);
End = Start+txtSearch.value.length;
if(Start==-1){
alert("已到达文件结尾!");
return;
}
if(document.selection){
var range=txtText.createTextRange();
range.collapse(true);
range.moveEnd("character",End);
range.moveStart("character",Start);
range.select();
}
else{
txtText.setSelectionRange(Start, End);
txtText.focus();
}
Start++;
}
</script>