移动光标到相应的文本(比如项目中,插入了图片之后,即在插入的地方显示光标)
2017-12-09 13:12
399 查看
在选定的文本框中,点击了光标,进行了额外的操作,比如说,进行图片的插入、表情的插入之后,如果是直接在改dom元素中进行focus操作的话,光标的位置会直接放置文本的最后,而不是我们期望的放置在插入位置的最后。
要想改变这个光标的正确的返回位置的话,那么就得封装一个方法,用来对光标位置的移动。
项目中的代码如下:(该项目建立在vue的基础上,用vue2的注册组件方法ref绑定了元素进行操作,具体看如下的注释)
...
sendEmoji (item) {//点击了标签图片的方法
let _self = this,//将vue用变量存储
msgTextarea = _self.$refs.msg_textarea,//vue的注册组件以后,调用DOM的方法
cursorIndex = 0,//光标在文本框中的位置
comment = _self.comment;//vue中的评论内容
if (document.selection) {
// IE Support(测试了一下,ie11也不走这里了,其它版本的ie没有细看)
msgTextarea.focus ();
let range = document.selection.createRange();
range.moveStart ('character', -msgTextarea.value.length);
cursorIndex = range.text.length;
}else if (msgTextarea.selectionStart || msgTextarea.selectionStart==0) {
// another support
cursorIndex = msgTextarea.selectionStart;
}
let commentLight = comment.substring(0, cursorIndex) + '【em'+item+'】';
_self.comment = commentLight + comment.substring(cursorIndex, comment.length);
this.$nextTick(function(){//方法解决异步问题,获取更新后的DOM
msgTextarea.focus();
msgTextarea.selectionStart = msgTextarea.selectionEnd = commentLight.length;//这是最终的状态让msgTextarea.selectionStart跟msgTextarea.selectionEnd相等,这里是关键,必须要获取到对的位置
})
this.emojiShow = false;
},
...
jq版:(这里可以修改成为js的代码,可以直接利用)
//文本框光标输入
(function($) {
$.fn.insertContent = function(myValue, t) {
var $t = $(this)[0];
if (document.selection) { //ie
this.focus();
var sel = document.selection.createRange();
sel.text = myValue;
this.focus();
sel.moveStart('character', -l);
var wee = sel.text.length;
if (arguments.length == 2) {
var l = $t.value.length;
sel.moveEnd("character", wee + t);
t <= 0 ? sel.moveStart("character", wee - 2 * t - myValue.length) : sel.moveStart("character", wee - t - myValue.length);
sel.select();
}
} else if ($t.selectionStart || $t.selectionStart == '0') {
var startPos = $t.selectionStart;
var endPos = $t.selectionEnd;
var scrollTop = $t.scrollTop;
$t.value = $t.value.substring(0, startPos) + myValue + $t.value.substring(endPos, $t.value.length);
this.focus();
$t.selectionStart = startPos + myValue.length;
$t.selectionEnd = startPos + myValue.length;
$t.scrollTop = scrollTop;
if (arguments.length == 2) {
$t.setSelectionRange(startPos - t, $t.selectionEnd + t);
this.focus();
}
}
else {
this.value += myValue;
this.focus();
}
};
})(jQuery);
要想改变这个光标的正确的返回位置的话,那么就得封装一个方法,用来对光标位置的移动。
项目中的代码如下:(该项目建立在vue的基础上,用vue2的注册组件方法ref绑定了元素进行操作,具体看如下的注释)
...
sendEmoji (item) {//点击了标签图片的方法
let _self = this,//将vue用变量存储
msgTextarea = _self.$refs.msg_textarea,//vue的注册组件以后,调用DOM的方法
cursorIndex = 0,//光标在文本框中的位置
comment = _self.comment;//vue中的评论内容
if (document.selection) {
// IE Support(测试了一下,ie11也不走这里了,其它版本的ie没有细看)
msgTextarea.focus ();
let range = document.selection.createRange();
range.moveStart ('character', -msgTextarea.value.length);
cursorIndex = range.text.length;
}else if (msgTextarea.selectionStart || msgTextarea.selectionStart==0) {
// another support
cursorIndex = msgTextarea.selectionStart;
}
let commentLight = comment.substring(0, cursorIndex) + '【em'+item+'】';
_self.comment = commentLight + comment.substring(cursorIndex, comment.length);
this.$nextTick(function(){//方法解决异步问题,获取更新后的DOM
msgTextarea.focus();
msgTextarea.selectionStart = msgTextarea.selectionEnd = commentLight.length;//这是最终的状态让msgTextarea.selectionStart跟msgTextarea.selectionEnd相等,这里是关键,必须要获取到对的位置
})
this.emojiShow = false;
},
...
jq版:(这里可以修改成为js的代码,可以直接利用)
//文本框光标输入
(function($) {
$.fn.insertContent = function(myValue, t) {
var $t = $(this)[0];
if (document.selection) { //ie
this.focus();
var sel = document.selection.createRange();
sel.text = myValue;
this.focus();
sel.moveStart('character', -l);
var wee = sel.text.length;
if (arguments.length == 2) {
var l = $t.value.length;
sel.moveEnd("character", wee + t);
t <= 0 ? sel.moveStart("character", wee - 2 * t - myValue.length) : sel.moveStart("character", wee - t - myValue.length);
sel.select();
}
} else if ($t.selectionStart || $t.selectionStart == '0') {
var startPos = $t.selectionStart;
var endPos = $t.selectionEnd;
var scrollTop = $t.scrollTop;
$t.value = $t.value.substring(0, startPos) + myValue + $t.value.substring(endPos, $t.value.length);
this.focus();
$t.selectionStart = startPos + myValue.length;
$t.selectionEnd = startPos + myValue.length;
$t.scrollTop = scrollTop;
if (arguments.length == 2) {
$t.setSelectionRange(startPos - t, $t.selectionEnd + t);
this.focus();
}
}
else {
this.value += myValue;
this.focus();
}
};
})(jQuery);
相关文章推荐
- js实现文本框文本域光标处插入图片文本的插件(并且光标在插入内容的内容后显示)
- 两种方法,针对EditText密码的显示和隐藏(包括激将光标移动到文本末尾)
- iOS开发小技巧--即时通讯项目:使用富文本在UILabel中显示图片和文字;使用富文本占位显示图片
- 鼠标在文本上移动时层的显示与消失(以及层里显示图片)ASP.NET
- windows server 2003服务器插入移动硬盘之后未显示盘符问题处理
- AJAX练习3--鼠标移动到指定的图片显示相应的该图片的信息
- 图片全部加载完成之后再显示页面ui,公司项目里用上,自己写的几行代码
- js在一个可编辑的div光标处插入图片或者文本(兼容ie,火狐等浏览器)
- HTML开发——项目心得(CSS实现导航栏选项卡炫酷渐变效果以及图片替换文本显示)
- 在emacs编辑的文本中插入图片 并直接在文本里显示该图片(转载)
- AJAX练习3--鼠标移动到指定的图片显示相应的该图片的信息
- UNIX文件目录管理基本命令之移动光标屏幕翻滚插入文本
- 图片上传生成缩略图,并使用JQuery实现鼠标移动到缩略图显示相应的大图的例子
- idea用tomcat发布项目时js/css/图片等静态内容显示问题
- javascript实现在textarea光标位置插入文字并移动光标到文字末尾
- 文本中显示图片的方法
- ios项目真机调试时图片不能显示
- 设置div中文字超出时自动换行和css实现文本超出N行之后显示省略号等
- 安卓移动文件(图片)到指定目录,并在相册中显示
- Ext Js 3.2修改文本颜色和在表格中插入图片,主要是性别进行判断来选择图片