admin管理员组文章数量:1391987
I have a target div el with some ponents displayed. These ponents have some events attached (on mouse over, on click). I don't control this code neither those events. They are just there. I'd like to render a widget inside this div. To do so I'm doing something like:
var save = el.innerHTML;
el.innerHTML = "my widget code";
When my widget finishes its process I want to get back to the original content so I do:
el.innerHTML = save;
The ponents previously saved are correctly replaced but the events attached to them don't work anymore.
I can't hide, show divs. I need to replace the innerHTML to have the exact style and positioning.
What would you do in my case? I'm using jQuery if it helps.
Thanks.
I have a target div el with some ponents displayed. These ponents have some events attached (on mouse over, on click). I don't control this code neither those events. They are just there. I'd like to render a widget inside this div. To do so I'm doing something like:
var save = el.innerHTML;
el.innerHTML = "my widget code";
When my widget finishes its process I want to get back to the original content so I do:
el.innerHTML = save;
The ponents previously saved are correctly replaced but the events attached to them don't work anymore.
I can't hide, show divs. I need to replace the innerHTML to have the exact style and positioning.
What would you do in my case? I'm using jQuery if it helps.
Thanks.
Share Improve this question edited Oct 3, 2012 at 16:51 Jordi P.S. asked Oct 3, 2012 at 16:45 Jordi P.S.Jordi P.S. 4,0287 gold badges39 silver badges59 bronze badges 1-
What exactly are you doing to attach the events?
innerHTML
doesn't bind/unbind events - that's why it's nice to use jQuery'shtml
,append
,empty
,replaceWith
and whatever else methods - they take care of cleaning up jQuery-bound events. The events that are binded (depending on whether you're using jQuery or not) may not reference an element when you overwrite them withinnerHTML
. What exactly is "my widget code"? – Ian Commented Oct 3, 2012 at 16:46
3 Answers
Reset to default 5When you serialize DOM elements back to HTML, all the event handlers and bound data are lost.
If you have DOM, work with it, not with HTML:
var save = $(el).children().detach();
$(el).html(widgetHTML);
// later
$(el).empty().append(save);
You might find .detach
useful - http://api.jquery./detach/
It does the same as .remove
but keeps all the associated jquery element data - which will include any event listeners added with jquery, though you will lose any 'regular dom events' attached.
you'd have to re-attach the element with jQuery too:
var stored = $(el).detach();
//… wait for something to finish, then reattach
stored.appendTo('#outer');
You can delegate the event to your el
element since it doesn't seem to change - Those newly added elements did not exist at the time of binding
$(el).on('click','yourelementtoattachevent',function(){
// replace click with your event/events
// your code goes here
});
This is assuming you are using jQuery 1.7+, for others use
$(selector).live(); // jQuery 1.3+
$(document).delegate(selector, events, handler); // jQuery 1.4.3+
$(document).on(events, selector, handler); // jQuery 1.7+
本文标签: javascriptReplace innerHTML and get back to the original one keeping the eventsStack Overflow
版权声明:本文标题:javascript - Replace innerHTML and get back to the original one keeping the events - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744638903a2616996.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论