开发者

Save Jquery Object without losing its binding

开发者 https://www.devze.com 2022-12-27 00:09 出处:网络
I have object created using jquery where each object has it\'s own binding. function closeButton(oAny){

I have object created using jquery where each object has it's own binding.

function closeButton(oAny){

var div = create_div();
$(div).attr("id","btn_"+$(oAny).attr("id"));
var my_parent = this;

$(div).html("<img src='"+ my_parent._base_url +"/assets/images/close.gif'>");
$(div).click(function(){
       alert("do some action here");
});

return div;

}


var MyObject = WindowObject();
var btn = closeButton(MyObject);

$(myobject).append(btn);
$("body").append(myobject); //at this point button will work as i expected    


//save to array for future use
ObjectCollections[0] = myobject;

//remove
$(myobject).remove();

$(body).append(ObjectCollections[0]); // at this point button will n开发者_StackOverflow社区ot work

For the first time i can show my object and close button is working as i expected. But if i save myobject to any variable for future use. It will loose its binding. Anybody ever try to do this ? Is there any work around ? or It is definitely a bad idea ? .And thanks for answering my question.


instead of click binding like this you need to use live function which keeps the events alive

$('yourelement').live('click', function(){
// some stuff
});


Have you tried using jQuery live?

It basically allows you to dynamically add objects and have the bindings preset for them. Try something like

$(div).live('click', function(){
 alert("do some action here");
});


I have try XGreen and Fudgey suggestion to use Live binding.

But still won't work. I have my own answer now. I will use static binding a.k.a onClick.

this is what I do.

$(btn).attr("onClick","someFunction('#"+ $(myParentObject).attr("id")+"')");

thanks for your answer XGreen and Fudgey.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号