【发布时间】:2011-07-18 00:03:35
【问题描述】:
更新:
你可以在http://jsfiddle.net/sV3us/1/看到代码
我得到了这个功能。它适用于非 IE 浏览器,但 IE9 不起作用。 我用这个函数调用 execCommandOnElement(testDiv, "insertHTML", "一些内容")
function execCommandOnElement(el, commandName, value) {
if (typeof value == "undefined") {
value = null;
}
if (typeof window.getSelection != "undefined") {
// Non-IE case
// Temporarily enable designMode so that
// document.execCommand() will work
document.designMode = "on";
//place caret
selection.collapse (anchorNode, anchorOffset);
selection.extend( focusNode, focusOffset);
// Execute the command
document.execCommand(commandName, false, value);
// Disable designMode
document.designMode = "off";
}
else if (typeof document.body.createTextRange != "undefined") {
close_modal ();
// IE case
var textRange = document.body.createTextRange();
textRange.moveToElementText(el);
document.execCommand(commandName, false, value);
}
}
我不需要在我的内容可编辑 div 中选择某些内容(例如将文本加粗),我只需要 IE 将 HTML 粘贴到插入符号所在的位置。因为我需要使用对话框来获取一些要粘贴的内容,所以我使用这个函数来知道放置它的位置。
//launch
$(".launch_modal").click(function() {
open_modal ( $(this).attr( 'data-modal' ) );
selection = window.getSelection() ;
anchorNode = selection.anchorNode;
anchorOffset = selection.anchorOffset;
focusNode = selection.focusNode;
focusOffset = selection.focusOffset;
});
非常感谢您的帮助。我整天都在这。
【问题讨论】:
-
好的,那么您在 IE9 中遇到特定错误了吗?
-
当我单击提交按钮时,对话框关闭,但可编辑 div 中没有任何内容,并且该 div 被 6 个正方形包围,可以调整大小。
-
你能创建一个jsfiddle这个吗
-
@Ibu jsfiddle.net/sV3us 如果可以,请告诉我。我清理了不重要的部分并省略了 css