div设置contentEditable="true"作为文本编辑器,定位光标解决办法
2015-09-01 16:21
609 查看
比如你会在div中插入表情,也可能输入文字,这时候,你可能需要定位光标的位置,默认不处理的情况下,div的光标是定位在文字后面,而你插入一个img标签,光标是不会跟随移动的!解决办法:
转自:/article/2256551.html
function set_focus(el) { el = el[0]; // jquery 对象转dom对象 el.focus(); if ($.browser.msie) { var rng; el.focus(); rng = document.selection.createRange(); rng.moveStart('character', -el.innerText.length); var text = rng.text; for (var i = 0; i < el.innerText.length; i++) { if (el.innerText.substring(0, i + 1) == text.substring(text.length - i - 1, text.length)) { result = i + 1; } } } else { var range = document.createRange(); range.selectNodeContents(el); range.collapse(false); var sel = window.getSelection(); sel.removeAllRanges(); sel.addRange(range); } }
转自:/article/2256551.html
相关文章推荐
- LeetCode Distinct Subsequences DP
- UILabel圆角、边框、宽度自适应
- QuickServer自学笔记(一)
- *** Assertion failure in -[CompensaCommentView _dequeueReusableViewOfType:withIdentifier:],
- 详解 CALayer 和 UIView 的区别和联系
- 使用pytest+requests进行接口测试实践——测试post请求
- MySQL高速缓存启动方法及参数详解query_cache_size=32M query_cache_type=1
- MySQL高速缓存启动方法及参数详解query_cache_size=32M query_cache_type=1
- MySQL高速缓存启动方法及参数详解query_cache_size=32M query_cache_type=1
- 利用Runtime修改UIdatePicker的字体颜色
- Android学习笔记之DocumentBuilder的使用....
- [Bluetooth(SDK)]使用WinSock连接到远程设备
- 使用pytest+requests进行接口测试实践——测试get请求
- #inception# Follow Your Heart and Persue Your Dream
- uva live 5031 Graph and Queries(Treap x 并查集)
- Myslq 启动报错The server quit without updating PID file
- 【iOS知识学习】_iPhone学习多个UITextField 键盘弹起
- [Bluetooth API]使用Irprops库搜索蓝牙设备
- iOS开发 UIScrollView自带的缩放功能(简单的例子)
- div设置contentEditable="true"作为文本编辑器,定位光标解决办法