就是编辑器里比如原来就有那么一段文字..
ABCDEFGHIJKLMNOPQRSTUVWXYZ
然后你在继续往里面添加输入内容的时候,新加入的文字要求默认换成其他颜色,比如你在A和B直接输入一些字就变成
ABCDEFGHIJKLMNOPQRSTUVWXYZ
绿颜色的就是新添加的文字,我想要控制输入文字的默认颜色...

解决方案 »

  1.   

    IE下可以,ff太麻烦了,我就不搞了:-)<html>
      <head>
        <meta http-equiv="Content-Type" content="text/html;charset=gb2312" />
        <title>Editor</title>
    <style>
    span.lostfocus{text-decoration:none;color:black;}
    span.focus{text-decoration:underline;color:red;font-weight:bold;}
    </style>
      </head>
      <body>
        <table width="700px" border="0" cellpadding="0" cellspacing="1" align="center">
         <tr><td><span class="focus" id='Mode1' onclick="SetMode(1,this)">模式1</span>  <span href="#" id='Mode2' class="lostfocus" onclick="SetMode(2,this)">模式2</span></td></tr>
         <tr><td>
           <iframe id="Editor" name="Editor" width="100%" height="300px" marginheight="0" marginwidth="0" frameborder="1"></iframe>
         </td></tr>
        </table>
    <script>var ns=navigator.appName=="Netscape";
    var Editor,EditorDoc;
    var Mode=1,Mode2Color="green",Mode1Color="black";function SetMode(m,o){
     Mode=m;
     $('Mode'+(m==1?2:1)).className="lostfocus";
     o.className="focus";
    }
    window.onload=function(){
     Editor=document.getElementById("Editor").contentWindow
     InitIframe("gb2312");
     EditorDoc=Editor.document;
     //事件处理
     if(document.all)EditorDoc.onkeydown=Handler;
    }
    function Handler(){
     var e=Editor.event;
     if(e.keyCode==37||e.keyCode==38||e.keyCode==39||e.keyCode==40)return;//方向键取消动作
     var color=eval("Mode"+Mode+"Color");
     var r=EditorDoc.selection.createRange();
     var p=r.parentElement(); 
     if(p.tagName!="SPAN"||!p.style||p.style.color!=color){
       r.pasteHTML('<span style="color:'+color+'">&nbsp;</span>');
       r.moveStart('character',-1);r.select();  //这句是关键,移动光标选择span中插入的空格,从而可以输入指定的颜色
     } 
    }function $(id){return document.getElementById(id);}
    function InitIframe(charset){   
     Editor.document.open(); 
     Editor.document.write("<html>");
     Editor.document.write("<head>");
     Editor.document.write("<style type='text/css'>body{font-size:12pt;}</style>");
     Editor.document.write("</head>");
     Editor.document.write("<body></body>");
     Editor.document.write("</html>");
     Editor.document.close(); 
     Editor.document.body.contentEditable="true";
     Editor.document.designMode="On";
     Editor.document.charset=charset;
     Editor.focus();
    }
    </script>
      </body>
    </html>