简体   繁体   中英

Add thead and insert first tr from tbody for parent table only

I have to create a thead using the first row from a tbody. I don't have full control of the markup so I'm using jQuery to do this.

I have been successful using some borrowed script but when I add more than one table to a page the first row seems to be being duplicated across the following tables

I would imagine that I need to target the parent table, normally I would give each table an ID but I cannot as these are generated for me.

an example of what's happening can be seen on this fiddle http://jsfiddle.net/FGH6B/

My current jquery is as follows

jQuery("document").ready( function() {

    var mytable = $("table"); 

    mytable.prepend(document.createElement('thead'));

    $("table thead").append($("tbody tr:eq(0)"));

});

Try this:

$("document").ready( function() {
    $('table').each(function(){
    $(this).prepend('<thead></thead>')
    $(this).find('thead').append($(this).find("tr:eq(0)"));
})});

Working Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM