<script>
function fun(){alert('ok')};
str="<input onclick='alert(this.value);fun()' value='i am ok'>xxxxxx...</font>......."
document.write(str)
</script>

解决方案 »

  1.   

    UP
    楼上的,要写入<script>…………</script>。
      

  2.   

    correct me if I am wrong, "<script>...</script>" does not seem to work in innerHTML. So you have only a few choices,1. create a new script element, for example
    <script language=javascript>
    str = "function fun() {alert('fun fun fun!');} fun();";function window.onload()
    {
      var s = document.createElement("script");
      s.defer = true;
      s.type="text/javascript";
      s.text = str;
      //document.all("dv").appendChild(s); 
     // or 
      document.body.appendChild(s);
    }
    </script>
    <div id="dv"></div>2. create an iframe and write the string into it, for example:
    <script language=javascript>
    str="<script language=javascript id='js1'>function fun() {alert('fun fun fun!');} fun();<\/script><font color='red'>xxxxxx...<\/font>.......";function window.onload()
    {
     var ifr = document.createElement("IFRAME");
      ifr.name="ifr";
      ifr.id="ifr";
      document.body.appendChild(ifr);
      frames["ifr"].document.open();
      frames["ifr"].document.write(str);
      frames["ifr"].document.close();}
    </script>3. or replace the current window, for example:
    <script language=javascript>
    str="<script language=javascript id='js1'>function fun() {alert('fun fun fun!');} fun();<\/script><font color='red'>xxxxxx...<\/font>.......";function replaceDoc()
    {
      document.open();
      document.write(str);
      document.close();
    }
    </script>
    <input type="button" value="replace" onclick="replaceDoc()">
      

  3.   

    谢谢大家的回复!大家可能没搞懂我的意思,这是一个江湖聊天室,所有的显示都是通过这种方式实现的:
      response.write str1
     而 str1="<script language=javascipt>
    document.writeln(str);
    </script>"
    我想在str中实现函数调用的功能,不知道怎么写!希望高手能给予指点!