function aa()
{
  alert("aaaaa"); 
}
function test()
{
  document.write('<img src="image/009.jpg" width=110 height=110 border="1" onclick="aa();">'); 
  

这段代码触发了test函数后,能正常显示009.jpg这张图片,但是这图片的onclick事件失效,点击图片之后不能触发到aa函数,不知道是什么原因。请教各位高手.

解决方案 »

  1.   

    代码没有任何问题。应该是你调用test();的方式出了问题了
    你调用test()以后,看到图片了。右键查看一下页面源代码,看看除了这张图片以外,其他的东西还在不在
      

  2.   

    function aa()
    {
      alert("aaaaa");  
    }
    function test()
    {
      document.write('<img src="images/0.gif" width=110 height=110 border="1" onclick="aa();">');  
       
    }test();可以触发啊
      

  3.   

    不要再文档流关闭后再使用document.write,要不write出来的内容会覆盖当前文档内容
      

  4.   

    改成
    function test()
    {
      document.getElementById('theforever_csdn').innerHTML='<img src="image/009.jpg" width=110 height=110 border="1" onclick="aa();">';  
       
    }  
     
    然后在BODY中增加一行
    <div id="theforever_csdn">图片将要显示在这里</div>
      

  5.   

    必然的,因为document.write破环了页面内容,aa函数没有了
    你应该把图片的html输出到一个标签,用innerHTML取代document.write
    或者创建html对象,用appendChild方式加载到页面:
    function aa()
    {
      alert("aaaaa");  
    }
    function test()
    {
      var img1=document.createElement("img");
      img1.src="image/009.jpg";
      img1.style.width='110px';
      img1.style.height='110px';
      img1.onclick=aa;
      document.body.appendChild(img1);   
      

  6.   

    我只是想说,这样使用,是可以的
    <html>
    <head>
        <title> 测试 </title>
        <script type="text/javascript">
        function aa(){
            alert("aaaaa");  
        };
        function test(){
            document.write('<img src="image/009.jpg" width=110 height=110 border="1" onclick="aa();">');  
        };
        test(); 
    </script>
    </head>
    <body>
    </body>
    </html>
      

  7.   

    test()放Body的Onload事件里,Write会覆盖掉之前文档的内容。
    可是摆在script里调用,却安然无恙。
    这不科学啊,求解释。
      

  8.   

    科不科学,习惯就好,我基本都不用document.write
      

  9.   


    我也是。印象中,我从未用过document.write
      

  10.   

    但楼主的,肯定是在一个按钮或者链接的事件中调用那个test(),所以造成这种情况。