Javascript iframe更改选择
我正在创建一个非常简单的rte,这是我的问题,我可以在iframe(designmode=true)中获取所选文本,但是我无法更改它 htmlJavascript iframe更改选择,javascript,iframe,rte,designmode,Javascript,Iframe,Rte,Designmode,我正在创建一个非常简单的rte,这是我的问题,我可以在iframe(designmode=true)中获取所选文本,但是我无法更改它 html 更改 脚本文件 function $(Str1){return document.getElementById(Str1);} rte() { var texteditor=$('texteditor'); textEditor.document.designMode="on"; textEditor.document.body
更改
脚本文件
function $(Str1){return document.getElementById(Str1);}
rte()
{
var texteditor=$('texteditor');
textEditor.document.designMode="on";
textEditor.document.body.contentEditable="True";
textEditor.document.open();
textEditor.document.close();
textEditor.focus();
}
function change()
{
var userSelection,range;
if (window.frames['textEditor'].getSelection)
{
userSelection=window.frames['textEditor'].getSelection();
}
else if(document.frames['textEditor'].selection)
{
userSelection=document.frames['textEditor'].selection.createRange();
}
if(userSelection.getRangeAt)
{
range=userSelection.getRangeAt(0);
}
else
{
range=document.frames['textEditor'].createRange();
range.setStart(userSelection.anchorNode,userSelection.anchorOffset);
range.setEnd(userSelection.focusNode,userSelection.focusOffset);
}
range="<div style='color:#f00;'>" + range + "</div>";
}
window.onload = rte();
function$(Str1){returndocument.getElementById(Str1);}
rte()
{
var texteditor=$('texteditor');
textEditor.document.designMode=“on”;
textEditor.document.body.contentEditable=“True”;
textEditor.document.open();
textEditor.document.close();
textEditor.focus();
}
函数更改()
{
var用户选择,范围;
if(window.frames['textEditor'].getSelection)
{
userSelection=window.frames['textEditor'].getSelection();
}
else if(document.frames['textEditor'].selection)
{
userSelection=document.frames['textEditor'].selection.createRange();
}
if(userSelection.getRangeAt)
{
range=userSelection.getRangeAt(0);
}
其他的
{
范围=document.frames['textEditor'].createRange();
range.setStart(userSelection.anchorNode,userSelection.anchorOffset);
setEnd(userSelection.focusNode,userSelection.focusOffset);
}
范围=“范围+”;
}
window.onload=rte();
函数的第一行是否引用了“texteditor”而不是“texteditor”?我找到了一种方法,change函数应该是这样的
function change()
if(document.selection)
{
sText=textEditor.document.selection.createRange();
sText.execCommand("createlink","","");
temp=sText.parentElement().innerHTML;
newNode=textEditor.document.createElement("h1");
replacement=sText.parentElement().replaceNode(newNode);
newNode.innerHTML=temp;
}
else if(document.getSelection)
{
sText=textEditor.window.getSelection();
myTag=textEditor.document.createElement("div");
myTag.setAttribute("class","bold");
sText.getRangeAt(0).surroundContents(myTag);
}
}
你是对的,但是当我把它们都设置为相同的时候,脚本会给出一个错误
function change()
if(document.selection)
{
sText=textEditor.document.selection.createRange();
sText.execCommand("createlink","","");
temp=sText.parentElement().innerHTML;
newNode=textEditor.document.createElement("h1");
replacement=sText.parentElement().replaceNode(newNode);
newNode.innerHTML=temp;
}
else if(document.getSelection)
{
sText=textEditor.window.getSelection();
myTag=textEditor.document.createElement("div");
myTag.setAttribute("class","bold");
sText.getRangeAt(0).surroundContents(myTag);
}
}