开发者

jQuery. Element width depending on its parent width

I have a pixel-sized table which cells' width depends on the content. There are input elements in the first row of the table and I need them to have the same width as they parents (). The idea is that those inputs' width should be calculated automatically and set in pixels with jQuer开发者_如何学Pythony. There are no classes nor id's for those cells and inputs. I'm able to set the width for one cell only using selectors with code like this:

var parentWidth = $(".foo").width();
$('td.foo input').width(parentWidth);

But as I said I need it to be more universal without using any classes etc. Can anyone help me with that? Thank you!


You could use a child selector like this

$('tr:first td > input').each(function(){
    $(this).width($(this).parent().width());
});

This code should select all td of the first row and give them the width of their parent.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜