asfman
android developer
posts - 90,  comments - 213,  trackbacks - 0
    function AddText(txt) {
    obj = document.getElementById("test");
    selection = document.selection;
    obj.focus();
    if (typeof obj.selectionStart != "undefined") {
        var opn = obj.selectionStart + 0;
        obj.value = obj.value.substr(0, obj.selectionStart) + txt + obj.value.substr(obj.selectionEnd);
    } else if (selection && selection.createRange) {
        var sel = selection.createRange();
        sel.text = txt;
        sel.moveStart("character", -txt.length);
    } else {
        obj.value += txt;
    }
}
posted on 2009-03-05 21:44 汪杰 阅读(255) 评论(2)  编辑 收藏 引用 所属分类: javascript

FeedBack:
# re: AddText
2009-04-16 10:53 | 汪杰
function test(){
var selection;
if (window.getSelection) {
selection = ''+window.getSelection();
} else if (document.selection) {
selection = document.selection.createRange().text;
}
return(selection);
}  回复  更多评论
  
# re: AddText
2009-04-16 15:13 | 汪杰
<input type="button" onclick="addText('---')" value="click" />
<textarea id="t" rows="5" cols="15">testtest</textarea>
<script type="text/javascript">
<!--
function addText(txt) {
obj = document.getElementById("t");
selection = document.selection;
obj.focus();
if (typeof obj.selectionStart != "undefined") {
var s = obj.selectionStart;
obj.value = obj.value.substr(0, obj.selectionStart) + txt + obj.value.substr(obj.selectionEnd);
obj.selectionEnd = s + txt.length;
} else if (selection && selection.createRange) {
var sel = selection.createRange();
sel.text = txt;
} else {
obj.value += txt;
}
}

//-->
</script>  回复  更多评论
  
只有注册用户登录后才能发表评论。

<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(15)

随笔分类(1)

随笔档案(90)

文章分类(727)

文章档案(712)

相册

收藏夹

http://blog.csdn.net/prodigynonsense

友情链接

最新随笔

搜索

  •  

积分与排名

  • 积分 - 457557
  • 排名 - 6

最新随笔

最新评论

阅读排行榜

评论排行榜