Jquery IE问题-在可编辑框架中插入html
我正在我的JSFIDLE上试用一些基本的WYSIWYG特性- 现在,我所要做的就是从“标记”框中获取用户输入,并在单击“可视”按钮时将其插入“可视”框中。可视框是可编辑的iframe 我的JSFIDLE示例在Firefox和Chrome浏览器上运行良好。在IE9和IE10上,文本出现在第二次尝试中。在标记框中键入一些文本后,我第一次单击VisualButton时,iframe将变得可编辑,但没有文本。如果我单击标记,然后再次单击Visual,我会在那里看到文本 下面是小提琴的javascript部分Jquery IE问题-在可编辑框架中插入html,jquery,internet-explorer,wysiwyg,designmode,Jquery,Internet Explorer,Wysiwyg,Designmode,我正在我的JSFIDLE上试用一些基本的WYSIWYG特性- 现在,我所要做的就是从“标记”框中获取用户输入,并在单击“可视”按钮时将其插入“可视”框中。可视框是可编辑的iframe 我的JSFIDLE示例在Firefox和Chrome浏览器上运行良好。在IE9和IE10上,文本出现在第二次尝试中。在标记框中键入一些文本后,我第一次单击VisualButton时,iframe将变得可编辑,但没有文本。如果我单击标记,然后再次单击Visual,我会在那里看到文本 下面是小提琴的javascript
function iframe_load() {
var txtBox = $("#txtMarkup");
var iframe = document.getElementById('iframe');
var contentWindow = iframe.contentWindow ? iframe.contentWindow : iframe.contentDocument.defaultView;
$(iframe).show();
$("#btnVisual").removeClass("notSelected").addClass("selected");
$("#btnMarkup").removeClass("selected").addClass("notSelected");
if (document.all) { //IE
contentWindow.document.designMode = 'On';
var range = contentWindow.document.body.createTextRange();
range.pasteHTML($(txtBox).val());
range.collapse(false);
range.select();
contentWindow.focus();
} else {
contentWindow.document.designMode = 'On';
contentWindow.document.execCommand('selectAll', null, null); //Required to prevent appending
try { //This throws an error in FireFox, but command is successful
contentWindow.document.execCommand('insertHtml', false, $(txtBox).val());
} catch (ex) {}
contentWindow.focus();
}
return false;
}
function iframe_hide() {
var txtBox = $("#txtMarkup");
var iframe = document.getElementById('iframe');
$(txtBox).show();
$(iframe).hide();
$("#btnMarkup").removeClass("notSelected").addClass("selected");
$("#btnVisual").removeClass("selected").addClass("notSelected");
return false;
}
提前谢谢 我想问题是,在显示iframe并使其可编辑后,您可能需要等待一段时间。这似乎有效: 演示: 代码:
从没想过!非常感谢你。
function iframe_load() {
var txtBox = $("#txtMarkup");
var iframe = document.getElementById('iframe');
var contentWindow = iframe.contentWindow ? iframe.contentWindow : iframe.contentDocument.defaultView;
$(iframe).show();
$("#btnVisual").removeClass("notSelected").addClass("selected");
$("#btnMarkup").removeClass("selected").addClass("notSelected");
contentWindow.document.designMode = 'on';
window.setTimeout(function() {
var doc = iframe.contentDocument || iframe.contentWindow.document;
if (doc.body.createTextRange) {
var range = doc.body.createTextRange();
range.pasteHTML(txtBox.val());
range.collapse(false);
range.select();
contentWindow.focus();
} else {
doc.execCommand('selectAll', null, null); //Required to prevent appending
doc.execCommand('insertHtml', false, txtBox.val());
}
contentWindow.focus();
}, 20);
return false;
}