V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
V2EX  ›  inyfee  ›  全部回复第 34 页 / 共 34 页
回复总数  662
1 ... 25  26  27  28  29  30  31  32  33  34  
❮ ❯
@ljbha007 HI,想再请问你一个问题,因为黏贴嘛,是黏贴在光标的位置 ,但是如上代码中,是默认认为黏贴的内容是在最后,但有时候是在中间某个位置 ,所以如何做到,把黏贴的内容插入到光标位置之后呢?
我在想,先取出光标之前的内容,光标之后的内容 ,再 把黏贴的内容 插入在中间。
但是问题,我不知道怎么取出光标的位置 ,在网上找了很多代码,都没有用。
比如 :

$.fn.getCursorPosition = function() {
var el = $(this).get(0);
var pos = 0;
if('selectionStart' in el) {
console.log('selectionStart');
pos = el.selectionStart;
} else if('selection' in document) {
console.log('selection');
el.focus();
var Sel = document.selection.createRange();
var SelLength = document.selection.createRange().text.length;
Sel.moveStart('character', -el.value.length);
pos = Sel.text.length - SelLength;
}
return pos;
}

两个if 都没有进去。
@ljbha007 谢谢。
1 ... 25  26  27  28  29  30  31  32  33  34  
❮ ❯
About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   835 Online   Highest 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 11ms · UTC 21:34 · PVG 05:34 · LAX 14:34 · JFK 17:34
♥ Do have faith in what you're doing.