javascript - After inserting a node, its onclick attribute is lost. How strange?
天蓬老师
天蓬老师 2017-05-19 10:26:24
0
2
473


        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. . .

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(2)
阿神

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

Ty80
    var td3=document.createElement("td");
    var hf=document.createElement("a");
    hf.innerHTML="删除";
    hf.href="javascript:;";
    
    
    hf.onclick=del(this); =>去掉引号,并且,这个this是什么?????
    
    td3.appendChild(hf);
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template