<!DOCTYPE html> <html> <title>Div 光标位置 插入 文字 或 HTML</title> <script language="javascript" type="text/javascript"> var range; function getPos() { range = window.getSelection().getRangeAt(0);//找到焦点位置 var span = document.createElement(‘span‘); span.innerHTML = ‘qqqqqqqqqqqqqqqqqqqqqqqqqq‘; range.insertNode(span);//在焦点插入节点 } </script> </head> <body> <div id="myDiv" style="width: 300px; height: 100px;border: black 1px solid; font-size:small; line-height:1; " onclick="getPos();" onkeyup="getPos();" contenteditable="true"> a b ccdfdfd dfdffddf f fdfdf </div> </body> </html>
时间: 2024-10-10 17:32:29