标签:
selection.setPosition(container,5); 设置光标位置--到尾端 selection.setPosition(container,0); 设置光标位置--到首端container 为 编辑容器dom,,5 为编辑器子元素的长度,
<!DOCTYPE html> <html lang=http://www.mamicode.com/"en"> <head> <meta charset=http://www.mamicode.com/"UTF-8"> <title>Title</title> <style> #app{ width:300px; height:100px; border: 1px solid red; } img{ width: 20px; } </style> </head> <body> <!--<div contenteditable=http://www.mamicode.com/"true" id=http://www.mamicode.com/"app"> <img src=http://www.mamicode.com/"a.jpg" alt=http://www.mamicode.com/""> 123 </div>--> <div contenteditable=http://www.mamicode.com/"true" id=http://www.mamicode.com/"app"> 11<img src=http://www.mamicode.com/"a.jpg" alt=http://www.mamicode.com/""><img src=http://www.mamicode.com/"a.jpg" alt=http://www.mamicode.com/""> 00 <span class=http://www.mamicode.com/"sp">123</span> </div> </body> <script> let app = document.querySelector(‘#app‘); app.focus(); let selection =http://www.mamicode.com/ getSelection(); selection.setPosition(app,5); </script> </html>