请帮助修复我的功能,存储一个元素的事件并删除它们,然后重新安装;
不知道问题出在哪里,函数只会删除元素的事件,不会恢复。
请帮忙修复。
HTML
<a href="#" id="a">a</a>
<a href="#" id="b">b</a>
JS
$("#a").hover(function(){alert("test")});
$("#b").click(function(){
Test_Handle('#a');
}
function Test_Handle(id) {
target = $(id);
if($.hasData(target.get(0))){ // if the elment has event || target = element
target[0].event_name = [];
target[0].event_handler = [];
events = $._data(target.get(0), 'events'); // take all events of the element
$.each(events, function(event_name, event_handler){
target[0].event_name.push(event_name);
target[0].event_handler.push(event_handler);
}); //store the events
target.off(); // delete the events
}else{ // has the elment no event
for(var i=0; i < target[0].event_handler.length; i++){
target.on(target[0].event_name[i], target[0].event_handler[i]);
} // re-store the events
target[0].event_name = [];
target[0].event_handler = []; //reset
}
}