You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用jQuery替换可编辑div内容后,如何防止光标按键后回到开头?

替换可编辑div内容后,如何防止按键时光标移至开头?

我用以下代码监听可编辑div的输入事件,替换内容后每次按键光标都会跳到开头:

$('.editme').on('change keyup', function(e){
    wordFilter($(this).text());
    $(this).text(wordFilter($(this).text()));
}); 

我试过用selectionStart和selectionEnd保存恢复光标位置,但在可编辑div上不起作用,有没有简单的jQuery实现方法?


更新:可行解决方案(需引入caret插件)

$('.editme').on('change keyup', function(e){
    var caretPos = $(this).caret();
    wordFilter($(this).text());
    $(this).text(wordFilter($(this).text()));
    $(this).caret(caretPos);
});

内容的提问来源于stack exchange,提问作者tms99

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 20:50:27