手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >JS在TextArea光标位置插入文字并实现移动光标到文字末尾
JS在TextArea光标位置插入文字并实现移动光标到文字末尾
摘要:=IE支持document.selection=Firefox,Chrome,Safari以及Opera都有selectionStart和s...

=IE支持document.selection

=Firefox,Chrome,Safari以及Opera都有selectionStart和selectionEnd属性

复制代码 代码如下:

function insertText(obj,str) {

if (document.selection) {

var sel = document.selection.createRange();

sel.text = str;

} else if (typeof obj.selectionStart === 'number' && typeof obj.selectionEnd === 'number') {

var startPos = obj.selectionStart,

endPos = obj.selectionEnd,

cursorPos = startPos,

tmpStr = obj.value;

obj.value = tmpStr.substring(0, startPos) + str + tmpStr.substring(endPos, tmpStr.length);

cursorPos += str.length;

obj.selectionStart = obj.selectionEnd = cursorPos;

} else {

obj.value += str;

}

}

function moveEnd(obj){

obj.focus();

var len = obj.value.length;

if (document.selection) {

var sel = obj.createTextRange();

sel.moveStart('character',len);

sel.collapse();

sel.select();

} else if (typeof obj.selectionStart == 'number' && typeof obj.selectionEnd == 'number') {

obj.selectionStart = obj.selectionEnd = len;

}

}

复制代码 代码如下:

<input type="button" value="插入文字"></input>

复制代码 代码如下:

<input type="button" value="移到末尾"></input>

【JS在TextArea光标位置插入文字并实现移动光标到文字末尾】相关文章:

DEFER怎么用?

表单的一些基本用法与技巧

Javascript实现图片轮播效果(二)图片序列节点的控制实现

jQuery实现表格行上下移动和置顶效果

jquery读取xml文件实现省市县三级联动的方法

Js和JQuery获取鼠标指针坐标的实现代码分享

基于jQuery插件实现环形图标菜单旋转切换特效

Javascript客户端脚本的设计和应用

JQuery中两个ul标签的li互相移动实现方法

JavaScript检测字符串中是否含有html标签实现方法

精品推荐
分类导航