search

Home  >  Q&A  >  body text

javascript - After inserting a node, its onclick attribute is lost. How strange?



        var td3=document.createElement("td");
        var hf=document.createElement("a");
        hf.innerHTML="删除";
        hf.href="javascript:;";
        hf.onclick="del(this)";
        td3.appendChild(hf);

The above code. After inserting the a tag into the td tag, the onclick attribute of the a tag disappears, as shown below:

Please explain what is going on?

I just tried changing the penultimate sentence to:

hf.setAttribute('onclick','del(this)');

The onclick attribute can be successfully added. . .

天蓬老师天蓬老师2755 days ago477

reply all(2)I'll reply

  • 阿神

    阿神2017-05-19 10:28:24

    The first way of writing is to bind events to the hf object, and the second way of writing is to add attributes to the a tag

    reply
    0
  • PHP中文网

    PHP中文网2017-05-19 10:28:24

        var td3=document.createElement("td");
        var hf=document.createElement("a");
        hf.innerHTML="删除";
        hf.href="javascript:;";
        
        
        hf.onclick=del(this); =>去掉引号,并且,这个this是什么?????
        
        td3.appendChild(hf);

    reply
    0
  • Cancelreply