开发者

insert div at a particular position

开发者 https://www.devze.com 2023-02-05 18:55 出处:网络
How to remove mynavbar and append tomain_table as the first element <div id=\"mynavbar\" > <div>Some content</div></div>

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);
0

精彩评论

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