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

前端 未结 4 994
生来不讨喜
生来不讨喜 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:43

    $("td")[0]
    

    Is a DOM Level element and not a jQuery Object. and .width() is a jQuery method, so you literally can't use it on that. Do either of the following:

    $("td").eq(0).width(); // On a jQuery object
    $("td")[0].offsetWidth; // On a DOM Level Element.
    

    To fetch the width of all the elements use .each

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

提交回复
热议问题