亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

javascript - Why does an event trigger when assigning null to an object?
滿(mǎn)天的星座
滿(mǎn)天的星座 2017-05-19 10:33:15
0
4
659

For example, the following code

var box = document.getElementById("box");
box.onclick = function(){
    console.log(111);
};
box = null;

Why can I still trigger the click event of the box when I assign the value of the box to null? What I think is that although the box is assigned to null later, since the event has been added, the processing of the event has nothing to do with the box itself? I don’t know if this is right. Why can I just assign box.onclick to null? Doesn’t onclick belong to the box? , I hope you guys can give me a reasonable answer, thank you.

滿(mǎn)天的星座
滿(mǎn)天的星座

reply all(4)
phpcn_u1582

The box here is just a variable

var box = document.getElementById("box");

Here is assigning the reference of the "box" element to the box. Your box = null是把nullassigning the value to the box below has no effect on the "box" element. It is recommended to read this article
Maybe what I said is not very clear, please forgive me.

過(guò)去多啦不再A夢(mèng)
var box = document.getElementById("box");//生成一個(gè)box指針, 指向element
box.onclick //element綁定點(diǎn)擊事件
box = null; // 清空指針 與element無(wú)關(guān)
PHPzhong

You just gave the nickname "Xiao Ming" to another person, but the damage you caused to the original "Xiao Ming" is still there...
As an experienced driver, I must correct your whitewashing steps:
1 , first cover up your sins

box.onclick = null;

2. It depends on the situation whether you want to destroy the body or not

box = null;
或
box.parentNode.removeChild(box);
阿神

In fact, box is just a "pointer" to a DOM element.

Your operation on the box attribute will affect the DOM element, but when you assign it to null, you only change the pointing of the box, but it does not mean that the DOM element is cleared.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template