jquery - Why remove delete everything? -
why if take off comment page doesn't show p elements anymore?
<body> <div> <p>content</p> </div> <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script> <script> <!-- (var = 0; < 10; i++) { var $p = $("div > p:last-child"); $p.clone().attr("nome", "p_" + i).text("nome p_" + i).appendto($p); /* if (i == 9) { $("div > p:first-child").remove(); } */ } //--> </script>
your div has 1 immediate p child, append other p elements to.
so these identical:
$("div > p:last-child"); $("div > p:first-child"); when this:
$("div > p:first-child").remove(); … you've removed appended to:
$("div > p:last-child"); if don't want behavior, append new p elements div itself:
$p.clone().attr("nome", "p_" + i).text("nome p_" + i).appendto('div');
Comments
Post a Comment