我的目标是替换文本并将其放在 tag 内。问题是“为什么要更改 'createHtmlCode' 的 createTextNode?”
var pannel = $("#text");
var $iframe = $("<iframe>").attr("src","iframe.php");
var iframe = $iframe.get(0);
pannel.html($iframe);
//BOLD BUTTON CLICK
$("#bold").click(function(){
iframe.contentWindow.focus();
iframe.focus();
iframe.contentDocument.execCommand('bold', false);
iframe.focus();
});
//H1 CLICK, HERE'S THE PROBLEM
// Replace the content with tags h1 + actual selection
$("#h1").click(function(){
var doc = iframe.contentWindow.document;
var selected = iframe.contentWindow.getSelection();
console.info(selected);
if(selected.rangeCount){
var range = selected.getRangeAt(0);
var content = range;
var text = new String(selected);
range.deleteContents();
range.insertNode(iframe.contentDocument.createTextNode('<h1>' + text + '<h1>'));
}
});