detect the widest cell w/ jQuery

前端 未结 5 899
挽巷
挽巷 2021-01-19 21:34

I have a table with different values. First column contains labels. I need to get the width of the widest label. I assume I need some sort of a loop, but then what?

5条回答
  •  北海茫月
    2021-01-19 22:32

    This should work - it will go through each of the tds (in myTable) and find the widest:

    var widest = 0;
    $("#myTable tr td:first").each(function()
    {
         widest = ($(this).width() > widest) : $(this).width() : widest;
    });
    

    alternatively:

    var widest = 0;
    $("#myTable tr td:first").each(function()
    {
         if($(this).width() > widest){
             widest = $(this).width()
         }
    });
    

提交回复
热议问题