2005年7月4日

[collection]JS里在光标位置插入字符


<
script language
=style="COLOR: #000000">Javascriptstyle="COLOR: #000000">>
style="COLOR: #0000ff">functionstyle="COLOR: #000000"> AddOnPos(obj, charvalue)
{
style="COLOR: #008000">//style="COLOR: #008000">obj代表要插入字符的输入框style="COLOR: #008000">
style="COLOR: #008000">//style="COLOR: #008000">value代表要插入的字符
style="COLOR: #000000">
obj.focus();
varstyle="COLOR: #000000"> r =style="COLOR: #000000"> document.selection.createRange();
style="COLOR: #0000ff">var ctr style="COLOR: #000000">=style="COLOR: #000000"> obj.createTextRange();
style="COLOR: #0000ff">var i;
style="COLOR: #0000ff">var s style="COLOR: #000000">= obj.value;

style="COLOR: #008000">//style="COLOR: #008000">注释掉的这种方法只能用在单行的输入框input内style="COLOR: #008000">
style="COLOR: #008000">//style="COLOR: #008000">对多行输入框textarea无效style="COLOR: #008000">
style="COLOR: #008000">//style="COLOR: #008000">r.setEndPoint("StartToStart", ctr);style="COLOR: #008000">
style="COLOR: #008000">//style="COLOR: #008000">i = r.text.length;style="COLOR: #008000">
style="COLOR: #008000">//style="COLOR: #008000">取到光标位置----Start----style="COLOR: #008000">
style="COLOR: #0000ff">varstyle="COLOR: #000000"> ivalue =style="COLOR: #000000"> "style="COLOR: #000000">&^asdjfls2FFFF325%$^&style="COLOR: #000000">";
r.text
style="COLOR: #000000">= ivalue;
i
style="COLOR: #000000">=style="COLOR: #000000"> obj.value.indexOf(ivalue);
r.moveStart(
"style="COLOR: #000000">characterstyle="COLOR: #000000">", style="COLOR: #000000">-style="COLOR: #000000">ivalue.length);
r.text
style="COLOR: #000000">= style="COLOR: #000000">"";
style="COLOR: #008000">//style="COLOR: #008000">取到光标位置----End----style="COLOR: #008000">
style="COLOR: #008000">//插入字符style="COLOR: #008000">
obj.value style="COLOR: #000000">=style="COLOR: #000000"> s.substr(style="COLOR: #000000">0,i) style="COLOR: #000000">+style="COLOR: #000000"> charvalue style="COLOR: #000000">+style="COLOR: #000000"> s.substr(i,s.length);
ctr.collapse(
truestyle="COLOR: #000000">);
ctr.moveStart(
"style="COLOR: #000000">characterstyle="COLOR: #000000">", i style="COLOR: #000000">+style="COLOR: #000000"> charvalue.length);
ctr.select();
}
style="COLOR: #000000"></style="COLOR: #000000">scriptstyle="COLOR: #000000">>

没有评论: