.width() giving error $(…)[0].width is not a function?

前端 未结 4 980
生来不讨喜
生来不讨喜 2021-01-19 05:01

I have a table, and I\'m trying to find the width of each td in the table. I\'ve tried all kinds of variations of: $(\"td\")[0].width(), but none of them work,

4条回答
  •  轮回少年
    2021-01-19 05:38

    while you said I'm trying to find the width of each td in the table so you need to loop through tds by using .each() ..

    $('td').each(function(){
       alert($(this).width());
    });
    

    and to select specific td there are more ways

    by using :eq() selector

    $('td:eq(0)');   // index start from 0
    

    or .eq() in jquery

    $('td').eq(0)        // index start from 0
    

    or :nth-child() selector

    $('td:nth-child(1)');    // index start from 1
    

提交回复
热议问题