选中的这段文字有可能是纯文字,也有可能是HTML文本。怎么样才能在它后面插入一些代码?

解决方案 »

  1.   

    <html>
    <body>
    <a href="#" onclick="javascript:abc.innerHTML='这里是插入的内容'">插入</a>
    <div id=abc></div>
    </body>
    </html>
      

  2.   

    TO:Tonglu(同路) 网页上选中的任意一段文字不一定<DIV>标记。
      

  3.   

    简单的说:就是我用鼠标在网页中任选一个字或一句话,然后在这句选中的话后面,加入HTML代码。
      

  4.   

    简单的说:就是我用鼠标在网页中任选一个字或一句话,然后在这句选中的话后面,用脚本加入一些HTML代码。这句话在网页的任一位置,并且不可能有DIV块之类的标记。
      

  5.   

    <script>
    function a()
    {
    var rng=document.selection.createRange();
    rng.text+="test";
    }
    </script>
    <a href="#" onclick="a()">asdasd</a>加些文字。
      

  6.   

    如果你想要直接能显示HTML代码的效果
    可以参看这样的例子,不过上面的可以在你的选择之后加上HTML代码了。<HTML>
    <BODY>
    <H1 unselectable="on">Creating a Link and Retrieving the URL</H1>
    <script>
    function AddLink()
    {//Identify selected text
    var sText = document.selection.createRange();
    if (!sText==""){
        //Create link
         document.execCommand("CreateLink");
         //Replace text with URL
         if (sText.parentElement().tagName == "A"){
           sText.parentElement().innerText=sText.parentElement().href;
           document.execCommand("ForeColor","false","#FF0033");
         }    
      }
    else{
        alert("Please select some blue text!");
      }   
    }
    </script>
    <P unselectable="on">Select any portion of the following blue text, such as "My favorite Web site". Click the button to turn the selected text into a link. The text will be changed to the URL that you specify.</P>
    <P style="color=#3366CC">My favorite Web site is worth clicking on. Don't forget to check out my favorite music group!</P><BUTTON onclick="AddLink()" unselectable="on">Click to add link</BUTTON>
    </BODY>
    </HTML>
      

  7.   

    TO: wind0209(风のFeeling)
      
      感谢你提供的这段程序,已经取得很大进展,但好象只能加入文本,不能插入HTML!如果要插入HTML该如何?
      

  8.   

    <html><head>
    <script>
    function add()
    {
      
      var a = document.createTextNode("text");
      aa.appendChild(a);
      
    }
    </script>
    </head><body><p id="aa" onmouseup="add()">111111111111111111132</p></body></html>
      

  9.   

    <HTML>
    <BODY>
    洒的非固定撒幅度撒发斯蒂芬洒的幅度所附算法规范化非好汉定界符国货<input onclick=alert(document.body.innerHTML)>
    </BODY>
    </HTML>
    <script>
    var htm="<input>"
    document.onclick=function(){document.body.innerHTML+='<DIV style="position:absolute;left:'+event.clientX+';top:'+event.clientY+'">'+htm+'</div>'}
    </script>
      

  10.   

    TO :zhaoweiwei(if(轩辕剑^倚天剑)) 
        插入的时候位置是不能确定的啊。TO:sw47(我爱编程,脑筋好好,嗷嗷嗷嗷嗷...)     插入到BODY后面,程序如何判断用户选择的是哪个字?文字可能有很多个相同的字或者相同的话。