gpt4 book ai didi

javascript - 如何向后移动光标

转载 作者:太空宇宙 更新时间:2023-11-04 16:08:09 26 4
gpt4 key购买 nike

当我按 Enter 时,光标位于 <mark>This 的开头。我想获取光标在 <p>Hi 开头。我怎样才能用 jQuery 实现这个目标?

<div id="summernote" contenteditable="true">
<p>This is first line</p>
<p>Hi <u id="text_id" style="cursor:pointer;"><mark>This is underlined</mark></u> This is second line<br></p>
</div>

最佳答案

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div id="summernote" contenteditable="true">
<p>This is first line</p>
<p>Hi <u id="text_id" style="cursor:pointer;"><mark>This is underlined</mark></u> This is second line<br></p>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).on("click","#text_id",function() {
var el = document.getElementById("summernote");
var range = document.createRange();
var sel = window.getSelection();
range.setStart(el.childNodes[3], 0);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
el.focus();
});
</script>
</body>
</html>

关于javascript - 如何向后移动光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41716112/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com