有一个 div:<div contenteditable="true"></div>
在 IE(7,8,9) 中,然后我单击一个按钮以使用 document.execCommand('insertImage')
插入图像方法。然后插入图像。到现在为止没有问题。
但是插入的图像是使用可调整大小的处理程序选择的。我怎样才能取消选择并在图像后面重新聚焦?
谢谢。
$button.click(function(){
document.execCommand("insertImage",false,'url');
});
最佳答案
更新了更简单的代码
这里有一些讨厌的代码可以做到这一点。在其他浏览器中,它似乎无论如何都可以工作,所以下面可以原样使用。
演示:http://jsfiddle.net/timdown/mr7Ac/6/
代码:
function insertImage() {
var sel = document.selection;
if (sel) {
var textRange = sel.createRange();
document.execCommand('insertImage', false, "someimage.png");
textRange.collapse(false);
textRange.select();
} else {
document.execCommand('insertImage', false, "someimage.png");
}
}
关于javascript - 在 IE 中将图像插入内容可编辑的 DIV 时如何重新聚焦?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11329982/