javascript - 使用execCommand插入后如何获取图像元素?

标签 javascript html contenteditable execcommand insert-image

我们使用execCommand插入图片后,有没有办法获取图片元素?例如

e.execCommand('insertimage',0,'ronaldo.png')

最佳答案

不要使用 insertimage,使用普通的旧 insertHTML 并为您要插入的元素提供一个 ID,以便您稍后可以引用它。 即,

function insertHTML(img) {
  var id = "rand" + Math.random();
  var doc = document.getElementById("editor");
  doc = doc.document ? doc.document : doc.contentWindow.document;
  img = "<img src='" + img + "' id=" + id + ">";

  if(document.all) {
    var range = doc.selection.createRange();
    range.pasteHTML(img);
    range.collapse(false);
    range.select();
  } else {
    doc.execCommand("insertHTML", false, img);
  }
  return doc.getElementById(id);
};

关于javascript - 使用execCommand插入后如何获取图像元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12507328/

相关文章:

javascript - 两个 javascript 不会在同一页面上执行

javascript - Bootstrap 主题下拉菜单失败

jquery - 光滑的旋转木马图像不垂直适合屏幕且不垂直居中

angularjs - 在页面刷新时使用 Angular JS ui.router html5Mode(true) 配置 Amazon S3 静态站点

javascript - 如何使用 JavaScript 将 <td> 标签添加到我的 html 表格中?

javascript - JS、HTML。谷歌美化。如何动态地将美化的html代码插入到html页面的contenteditable div中

html - Google Docs如何实现内容编辑?

javascript - Redis不会立即删除key

javascript - 使用 javascript 在客户端打印 PDF 文件

javascript - 如何防止 'Enter' 在标记为 contentEditable 的 div 中工作?