insert div at a particular position
How to remove mynavbar and append to main_table as the first element
<div id="mynavbar" > <div>Some content</div> </div>
<div id="main_table"> </div>
<sc开发者_如何学运维ript>
if(condtion == 0)
{
// How to remove mynavbar and insert into main_table div as the first element
}
</script>
$("#mynavbar").prependTo("#main_table");
$('#mynavbar').prependTo('#main_table').remove();
$('#main_table').prepend ( $('#mynavbar').html() );
$('#mynavbar').remove ();
Here's a non-jQuery answer for some variety.
var main = document.getElementById("main_table");
var secondary = document.getElementById("mynavbar");
if(main.firstChild)
{
main.insertBefore(secondary, main.firstChild);
console.log("first");
}else
{
main.appendChild(secondary);
console.log("last");
}
EDIT: I'm amazed that there's 5 nearly identical answers. Yikes.
for remove you can simply use...
$("#mynavbar").remove();
but once it will removed, it will not be there in your html..you should try something like this...
var mynavbarHTML= $("#mynavbar").html();
$("#mynavbar").remove();
$("#main_table").prepend(mynavbarHTML);
精彩评论