如何获取光标的位置
只适配webkit
<p id="content" style="border: 1px solid #f1f1f1;" contenteditable="true"></p>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
如果是要获取的是光标偏移了多少个文字的话,可以通过
selectionStart属性来获取,webkit系的浏览器都支持这一属性。摘一个 stackoverflow 上的栗子:
http://ichord.github.io/Caret.js/
我记得Selection对象还有Range对象应该能获取到,详细的你可以查一下MDN