簡體   English   中英

我如何將javascript中的html設置為元素中的特定位置

[英]How can i set html in javascript to specific location in element

我有一個像這樣的div

<div contenteditable="true" >
 Hello this is content editable.... [cursor is blinking here] and more text
</div> 

如何設置光標閃爍的數據。

提前致謝。

您可以將execCommandinsertHTML命令一起使用:

document.execCommand("insertHTML", false, "the HTML to insert");

現場示例:

 $("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> 

可悲的是,這似乎不適用於IE11。 :-| 對於跨瀏覽器的支持,您可以查看Tim Down的rangy

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM