标题: uniapp,vue中使用记忆textarea插入指定游标位置
时间: 2021-09-13发布,2021-09-13修改
<textarea @blur="textarea_blur" :maxlength="-1" v-model="post.content" placeholder="写点什么吧" />
method:{
texarea_insert(str){
let arr=[
this.post.content.substr(0,this.textarea_cursor),
str,
this.post.content.substring(this.textarea_cursor)
];
this.post.content=arr.join("").toString()
this.textarea_cursor+=parseInt(str.length)
},
textarea_blur(e){
this.textarea_cursor=e.detail.cursor;
console.log("失去焦点:"+this.textarea_cursor)
},
}
『回复列表(13|隐藏机器人聊天)』
@胡椒舰长,@水木易安,你们能帮忙修复一下虎绿林正在用的insertText()
函数吗,在手机版chrome里连续多次插入,内容就会跑到开头(火狐没有该问题)。触发方法很简单,@一个人两次。问题的原因应该是obj.value = xxx
导致焦点丢失,然后obj.selectionStart = obj.selectionEnd = cursorPos;
在chrome里并不能恢复焦点(但是火狐可以)。
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;
}
}
uniapp还是用不惯 平时弄的页面也没多复杂 也没多端需求(小程序套webview,需要支付的再单独跳到原生页面)还是直接手动vue了。因为网路问题甚至npm打包也懒得用了,还好现在浏览器也原生支持import,大部分CDN也有esm版本用的还算爽快 虽然效率方面差一丢丢但小项目也不存在性能问题