[英]How can i set html in javascript to specific location in element
I have a div just like this 我有一个像这样的div
<div contenteditable="true" >
Hello this is content editable.... [cursor is blinking here] and more text
</div>
How can i set a data where cursor is blinking. 如何设置光标闪烁的数据。
Thanks in advance. 提前致谢。
You use execCommand
with the insertHTML
command: 您可以将execCommand
与insertHTML
命令一起使用:
document.execCommand("insertHTML", false, "the HTML to insert");
Live Example: 现场示例:
$("div").on("keydown", function(e) { if (e.ctrlKey && e.which == 81) { document.execCommand("insertHTML", false, "<strong>inserted</strong>"); return false; } });
<p>Put your cursor in the relevant location below, then press Ctrl+Q:</p> <div contenteditable="true"> Hello this is content editable.... and more text </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Sadly this doesn't seem to work on IE11. 可悲的是,这似乎不适用于IE11。 :-| :-| For cross-browser support, you might look at Tim Down's rangy
library . 对于跨浏览器的支持,您可以查看Tim Down的rangy
库 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.