开发者

selector - insertBefore()

开发者 https://www.devze.com 2023-03-06 11:29 出处:网络
how can you use insertBefore() when you don\'t have the id, class or anything for the table but only the object?

how can you use insertBefore() when you don't have the id, class or anything for the table but only the object?

var tbl_elm = $('the table');

$('<tr><td>txt</td></tr>').insertBefore(tbl_elm.eq(0));

somehow I need to tell the eq() that the index ha开发者_如何学Cs to be found in a 'TR' tag


Try this:

var tbl_elm = $("TABLE");    
$('<tr><td>txt</td></tr>').insertBefore($("TR", tbl_elm).eq(0))

However, if the elements you are adding to the table will always be placed before the first row, the below code is better practice:

var tbl_elm = $("TABLE");
$('<tr><td>txt</td></tr>').prependTo(tbl_elm)
0

精彩评论

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