我有一个WYSIWYG编辑器,可以在IE(所有版本)以外的所有浏览器中使用。我对以下功能有疑问:
function tag(tag) {
var range = window.frames['textEditor'].getSelection().getRangeAt(0);
var newNode = document.createElement(tag);
range.surroundContents(newNode);
}
有关如何解决此问题的任何想法?
答案 0 :(得分:2)
IE不支持范围,因为它是一个大婴儿。但是,它会做你想要的,但使用单独的逻辑。查看Quirksmode Range Page以获取有关如何将IE打造成形状的信息。
答案 1 :(得分:2)
IE< 9不支持标准DOM范围和选择。要在这些和所有主流浏览器中使用这些API的实现,您可以使用我的Rangy库。
另外,使用范围的surroundContents()
方法仅在范围开始和结束于同一节点或同一节点的子节点时才有效。例如,它不适用于以下范围(范围边界用方括号表示):
One |two <b>three| four</b>
要设置这样的范围,你需要一种更微妙的方法。
答案 2 :(得分:0)
我现在可以在所有浏览器中工作,如果有人需要脚本我将发布它
function changeHeading(heading) {
if($.browser.msie && $.browser.version == 7 || $.browser.version == 8)
{
theSelection = window.frames['textEditor'].document.selection.createRange().htmlText;
window.frames['textEditor'].document.selection.createRange().pasteHTML("<"+heading+">"+theSelection+"</"+heading+">")
$("#textEditor").contents().find(heading).unwrap();
}
else {
var needle =window.frames['textEditor'].getSelection().getRangeAt(0);
var haystack = $('#textEditor').contents().find("body").text();
var newText = haystack.replace(needle,"<"+heading+">"+needle+"</"+heading+">");
$('#textEditor').contents().find("body").html(newText);
$("#textEditor").contents().find(heading).unwrap();
}
}
感谢您的帮助