Simple javascript/jquery append, remove question
Hey guys, quick question. I have a div that on click will execute a jquery function onclick, and then replace the div with its opposite div which onclick will replace back to the first div just like the favorite function on this site. I am having a simple problem I hope someone can spot or explain to me it is not possible. The function works with the initial two divs that are served directly, but when I click on either of the original two divs, and the function executes and replaces the div with the corresponding div the replacement div does not execute the function. I h开发者_开发知识库ave to refresh the page to get the original div, which is identical to execute the function. Is the div actually replaced on the page with append, or does it just visually show it? Any advice appreciated.
original div
<div class="unfavorite"><img id="unfavorite_img" src="images/favorite2.png" /></div>
javascript div replacement
$(".favorite").remove();
$(".favoriteholder").append('<div class="unfavorite"><img id="unfavorite_img" src="images/favorite2.png" /></div>');
Instead of removing, hide them. Because, once you remove the DOM element, until the page is refreshed that DOM element is gone!, so you can't actually access it.
Other alternative is, have a temp div that holds this content (content for both divs) and get the content from the div to append to the present div or to create the removed div
精彩评论