我们使用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/