CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Web 开发 >  JavaScript

为何动态生成的页面,无法执行js代码呢?

楼主tuzai()2006-01-23 10:20:38 在 Web 开发 / JavaScript 提问

<html>  
  <head>  
  <script>  
  function   sde()  
  {  
      alert("ShowVcardJID");              
   
  }  
   
  function   init()  
  {  
        var   str   =   '<div><A   href="javascript:void(0)"   onclick="sde();">test!</div>';  
        document.write(str);        
  }  
   
   
   
  </script>  
  </head>  
   
  <body>  
  <div><A   href="#"   onclick="sde();">test!</div>     (此句能执行sde())  
  <input   type="button"   onClick="init();"   value="init"/>   (动态生成的页面,无法执行sde()函数)  
   
  </body>  
  </html> 问题点数:20、回复次数:5Top

1 楼chouchy(城市刀客)回复于 2006-01-23 10:29:29 得分 5

执行   init()   后,页面中不再存在sde()!  
  使用“   document.write”后页面内容被重写了Top

2 楼hbhbhbhbhb1021(天外水火(我要多努力))回复于 2006-01-23 10:30:51 得分 5

你重写的页面中已经不包含那个JAVASCRIPT函数了  
  按照你的思路需要再写一遍Top

3 楼hbhbhbhbhb1021(天外水火(我要多努力))回复于 2006-01-23 10:31:55 得分 5

<html>  
  <head>  
  <script>  
  function   sde()  
  {  
      alert("ShowVcardJID");              
   
  }  
   
  function   init()  
  {  
        var   str   =   '<script>function   sde(){alert("ShowVcardJID");}</scr'+'ipt><div><A   href="javascript:void(0)"   onclick="sde();">test!</a></div>';  
        document.write(str);        
  }  
   
   
   
  </script>  
  </head>  
   
  <body>  
  <div><A   href="#"   onclick="sde();">test!</div>     (此句能执行sde())  
  <input   type="button"   onClick="init();"   value="init"/>   (动态生成的页面,无法执行sde()函数)  
   
  </body>  
  </html>Top

4 楼net_lover(【孟子E章】)回复于 2006-01-23 10:32:11 得分 5

你应该这样  
  x.innerHTML   =   '<div><A   href="javascript:void(0)"   onclick="parent.sde();">test!</a></div>';  
       
   
  <div   id=x></div>  
  Top

5 楼tuzai()回复于 2006-01-23 11:13:17 得分 0

找到了:    
  Document.write(   )   is   usually   used   in   one   of   two   ways.   First,   it   can   be   invoked   on   the   current   document   within   a   <script>   tag   or   within   a   function   that   is   executed   while   the   document   is   being   parsed.   In   this   case,   the   write(   )   method   writes   its   HTML   output   as   if   that   output   appeared   literally   in   the   file   at   the   location   of   the   code   that   invoked   the   method.    
   
  Second,   you   can   use   Document.write(   )   to   dynamically   generate   the   contents   of   a   document   for   a   window   other   than   the   current   window.   In   this   case,   the   target   document   is   never   in   the   process   of   being   parsed,   and   so   the   output   cannot   appear   "in   place"   as   it   does   in   the   case   just   described.   In   order   for   write(   )   to   output   text   into   a   document,   that   document   must   be   open.   You   can   open   a   document   by   explicitly   calling   the   Document.open(   )   method.   In   most   cases   this   is   unnecessary,   however,   because   when   write(   )   is   invoked   on   a   document   that   is   closed,   it   implicitly   opens   the   document.   When   a   document   is   opened,   any   contents   that   previously   appeared   in   that   document   are   discarded   and   replaced   with   a   blank   document.    
   
  Top

相关问题

  • 求生成aspx页面代码!
  • 急找一个利用JavaScript对页面上生成的表格进行排序的js源代码。
  • 50大洋求ASP生成JS代码?
  • 老生长谈:Js生成树代码.
  • 打印分页问题 (代码里直接生成页面)
  • _____ 关于生成静态页面的一段代码,谢谢
  • 急jscript!在html页面自动“另存为”时生成的*.js代码中还有一个showtable.js文件,原因是原JSP页面内含有表格帮忙解释一下以下代码
  • 代码生成
  • 用模板生成静态页面时html代码怎么换行。
  • 100分求asp.net[要vb语言的]生成html页面的代码,

关键词

  • 页面
  • 执行
  • 执行sde
  • 动态生成的页面

得分解答快速导航

  • 帖主:tuzai
  • chouchy
  • hbhbhbhbhb1021
  • hbhbhbhbhb1021
  • net_lover

相关链接

  • Web开发类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo