javascript - 如何将 javascript 中的 html 设置为元素中的特定位置

标签 javascript jquery html

我有一个像这样的div

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

如何设置光标闪烁的数据。

提前致谢。

最佳答案

您使用 execCommand使用 insertHTML 命令:

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's rangy library .

关于javascript - 如何将 javascript 中的 html 设置为元素中的特定位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33592751/

相关文章:

html - 将父样式应用于框架中的页面

javascript - 如何在没有表单的情况下将图像上传到 Node js 服务器?

javascript - 仅在第一次加载页面时如何执行 Javascript 警报

javascript - 使用 "on()"时,事件数据在 jquery 中返回空对象

jquery - 我无法使菜单下拉淡入淡出效果起作用。 (初学者)

html - 在移动 View 中将 Bootstrap 卡居中

javascript - 有setClientBoundingRect()吗?

javascript - 将 angularjs 集合的项目的元素传递给 javascript 函数

JQuery 动画闪烁

javascript - 剪切路径圆弧形状