<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title></title></head>
<body><img id="a" onclick="alert('我是原始的')"\>

</body>
<script>
window.onload=function()
{
    var a=document.getElementsByTagName("img")[0];
    var html="<div onclick='"+a.onclick+"'>新按钮</div>";          //这里怎么写才正确呢。。
    a.outerHTML=html;
}
</script>
</html>

解决方案 »

  1.   

    <html>
    <head>
    <script type="text/javascript">function Test(obj)
    {
    document.getElementById('click2').onclick = obj.onclick;
    alert("请点测试");
    }
    </script>
    </head>
    <body>
    <a id="click1" href="javascript:void(0);" onclick="Test(this);">打印</a>
    <a id="click2" href="javascript:void(0);" onclick="">测试</a>
    </body>
    </html>
      

  2.   

    楼主你通过a.outerHTML=html以后id为a的元素都没了还怎么a.onclick啊
      

  3.   

    用个外部的函数多好啊function a() {
        alert('我是原始的');
    }<img id="a" onclick="a();"\><script>
    window.onload=function()
    {
        var a=document.getElementsByTagName("img")[0];
        var html="<div onclick='a();'>新按钮</div>";         
        a.outerHTML=html;
    }
    </script>
      

  4.   

    1、定义一个函数,直接调用
    2、obj1.onclick = obj2.onclick;
      

  5.   

    补充一下:
    这个div是动态生成的,不一定有id,故不用getElementByID来获取这个对象,另外,由于现实中img可能具有可变的属性,所以也不能用createElement来创建,只能是在这行代码中修改从而使得它具有a的事件    var html="<div onclick='"+【关键是这里怎么写】+"'>新按钮</div>";  
    a.onclick能获得到a的onclick事件没问题,只是ie下弄成了个字符串,ff正常的函数在ff中没有问题,因为ff中
    a.onclick就是Test(this);在ie中,a.onclick成为了
    function anonymous()
    {
      Test(this);
    }
      

  6.   

    getAttribute('onclick')
    setAttribute('onclick', ...)
      

  7.   

    楼上,利用getAttribute('onclick')其实跟a.onclick一样的,ie下也是字符串