For doubts about the event, please look at the code.

var $buttom=[];
        
//
function addBtn() {
    var $dom = $("<button> " + iPP + "</button>");
    $dom.on("click",function(){
        console.log($(this).html());
    });
    $buttom.push($dom);
    $.each($buttom,function(i,n){
        $("-sharptest1").append(n);
    });
};
//
function del(){
    $("-sharptest1").empty();
};
//
function recov(){
    $.each($buttom,function(i,n){
        $("-sharptest1").append(n);
    });
};
//2
function resetEve(){
    $.each($buttom,function(i,n){
        n.on("click",function(){
            console.log($(this).html());
        });
    });
 
    del();

    $.each($buttom,function(i,n){
        $("-sharptest1").append(n);
    });
};
        
//1:
//:,log,,.
addBtn();
del();
recov();
        
//2:
//:,log,del(),resetEve(),,
//,del().append,.
addBtn();
del();
resetEve();
        
//3:
//:,log,,resetEve(),,
//,del().append,.
addBtn();
//del();
resetEve();

problem: events bound by $dom, via $dom saved with variables will lose their effect with empty ();

  1. Why is the event bound through the $dom variable lost? the variable name is still there. In general understanding, its event should also be there. Why is it lost?
  2. Why can I store this $dom variable through an array in mode 2? The array in this is obviously a pointer, pointing to the variable of $dom. I can"t think of the difference. But there must be something different. What"s the difference?

with regard to events, there must be something wrong with me. Or rely on the place, hope the master to show the way.


look at the document . Empty () | jQueryAPI Chinese document :

to avoid memory leaks, jQuery first removes the data and event handlers of the child elements, and then removes the child elements.
if you want to delete elements without destroying their data or event handlers (the bound information can be added back later), use .detach () instead.

in addition, it is recommended that you console the array. JQ encapsulates a lot of things. If you learn the native words, you can compare them with the native ones.


Action 2 and Action 3 are essentially the same. The essence of the problem is that after the dom object is deleted, the associated events are also deleted.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b306a0-2bcf5.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b306a0-2bcf5.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?