<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>兼容IE和FF的UBB简单编辑器</title>
</head>
<body onload="init();">
<iframe id="HtmlEditor" style="width:300px;height:300px;border:2px solid #000000;" src="about:blank"></iframe>
<input type="button" value="[reply]" onclick="aa()" />
<input type="button" value="insertHTML" onclick="insertHTML('<br />')" />
<script language="javascript">
var ie=document.all||false;
function $(id){ return document.getElementById(id);}
function init() { $("HtmlEditor").contentWindow.document.designMode="on";}
function aa()
{
 $("HtmlEditor").contentWindow.focus();
 var Range;
 if(ie)
 {
  Range=$("HtmlEditor").contentWindow.document.selection.createRange();
  Range.text="[reply]"+Range.text+"[/reply]";
 }
 else
 {
  Range=$("HtmlEditor").contentWindow.document.getSelection ? $("HtmlEditor").contentWindow. document.getSelection() : window.getSelection();
  //Range.text="[hide]"+Range.text+"[/hide]";
  alert(Range.toString());
 }
}
function insertHTML(html) {
 $("HtmlEditor").contentWindow.focus();
 if(ie)
  $("HtmlEditor").contentWindow.document.selection.createRange().pasteHTML(html) ; 
 else
  alert($("HtmlEditor").contentWindow.document.getSelection().toString());
}
</script>
</body>
在IE下没啥问题,FF下。。不知道如何弄。。JS刚入门

解决方案 »

  1.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>兼容IE和FF的UBB简单编辑器</title>
    </head>
    <body onload="init();">
    <iframe id="HtmlEditor" style="width:300px;height:300px;border:2px solid #000000;" src="about:blank"></iframe>
    <input type="button" value="[reply]" onclick="aa()" />
    <input type="button" value="insertHTML" onclick="insertHTML('<br />')" />
    <script language="javascript">
    var ie=document.all||false;
    function $(id){ return document.getElementById(id);}
    function init() { $("HtmlEditor").contentWindow.document.designMode="on";}
    function aa()
    {
     $("HtmlEditor").contentWindow.focus();
     var Range;
     if(ie)
     {
      Range=$("HtmlEditor").contentWindow.document.selection.createRange();
      Range.text="[reply]"+Range.text+"[/reply]";
     }
     else
     {
      Range=$("HtmlEditor").contentWindow.document.getSelection ? $("HtmlEditor").contentWindow. document.getSelection() : window.getSelection();
      //Range.text="[hide]"+Range.text+"[/hide]";
      alert(Range.toString());
     }
    }
    function insertHTML(html) {
     $("HtmlEditor").contentWindow.focus();
     if(ie)
      $("HtmlEditor").contentWindow.document.selection.createRange().pasteHTML(html) ; 
     else
      alert($("HtmlEditor").contentWindow.document.getSelection().toString());
    }
    </script>
    </body>
      

  2.   

    ff下没有.contentWindow直接.document即可