今天偶尔有兴趣想写个range,难的下不了手,写了个简单的。。。,然而我发现,当要搜索的文本对象大过20k的时候,会造成cpu的使用率飙升。。。大家试试看。。应该不是我的机子的问题,而是程序写的不够优化。。。呵呵。。
脚本如下:
function movePoint()
{
var pn = parseInt(pnum.value);
if(isNaN(pn))
return;
var rng = box.createTextRange();
rng.moveStart(“character”,pn);
rng.collapse(true);
rng.select();
returnCase(rng)
}
function tellPoint()
{
var rng = event.srcElement.createTextRange();
rng.moveToPoint(event.x,event.y);
rng.moveStart(“character”,-event.srcElement.value.length)
pnum.value = rng.text.length
returnCase(rng)
}
function returnCase(rng)
{
bh.innerText = rng.boundingHeight;
bl.innerText = rng.boundingLeft;
bt.innerText = rng.boundingTop;
bw.innerText = rng.boundingWidth;
ot.innerText = rng.offsetTop;
ol.innerText = rng.offsetLeft;
t.innerText = rng.text;
}
function selectText(sp,ep)
{
sp = parseInt(sp)
ep = parseInt(ep)
if(isNaN(sp)||isNaN(ep))
return;
var rng = box.createTextRange();
rng.moveEnd(“character”,-box.value.length)
rng.moveStart(“character”,-box.value.length)
rng.collapse(true);
rng.moveEnd(“character”,ep)
rng.moveStart(“character”,sp)
rng.select();
returnCase(rng);
}
var rg = box.createTextRange();
function findText(tw)
{
if(tw==””)
return;
var sw = 0;
if(document.selection)
{
sw = document.selection.createRange().text.length;
}
rg.moveEnd(“character”,box.value.length);
rg.moveStart(“character”,sw);
if(rg.findText(tw))
{
rg.select();
returnCase(rg);
}
if(rg.text!=tw)
{
alert(“搜索完毕!”)
rg = box.createTextRange()
}
}
[/java]