我新建了一个DOM元素:var oNewA = document.createElement("a");  然后想为他加上一个onclick的响应事件但试了很都方法都不成功,比如:oNewA.onclick = "change('"+oNewA.id+"')"; 或者:oNewA.attachEvent('onclick',change(oNewA.id));   请求指教,小弟很穷没什么分,希望大家原谅,谢谢了

解决方案 »

  1.   

    function change(id) {
        ....
    }oNewA.onclick = function() {
        ....
        change(oNewA.id);
        ....
    }
      

  2.   

    主要是楼主把onclick直接赋值,是不行的,应该是oNewA.onclick=function(){change(this.id)}
      

  3.   

    var   oNewA   =   document.createElement( "a ");
    oNewA.id ="hello";
    oNewA.onclick = new Function("change(this.id)");
    function change(id)
    {
       alert(id);
    }
      

  4.   

    <html><head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>新建网页 1</title>
    </head><body onload="test()"><script>
    function test()
    {
    var   oNewA   =   document.createElement( "a");  
    oNewA.innerText = "sdfsdfsdf";
    oNewA.id="a1";
    document.body.appendChild(oNewA);
    oNewA.onclick = new Function("change(this.id)");
    }
    function change(id)
    {
       alert(id);
    }
    </script>
    </body></html>
      

  5.   

     document.createElement( 还没用过呢
    主要执行什么功能啊
      

  6.   

    完整的添加事件
            if(oNewA.addEventListener){//2级DOM规范浏览器
                oNewA.addEventListener("click",function(){change(oNewA.id);},true);
            }else if(oNewA.attachEvent){//IE5+
                oNewA.attachEvent("onclick",function(){change(oNewA.id);});
            }else{//IE4
                oNewA.onclick=function(){change(oNewA.id);};
            }