jquery to find all exact td matches

前端 未结 5 1485
陌清茗
陌清茗 2021-02-09 00:31
$(\'#servertable td:eq(\' + server + \')\')

this finds only 1 (first I think) match, how to find all matches. btw. td:contains will not work for me.

5条回答
  •  梦如初夏
    2021-02-09 00:52

    I could be wrong, but the :eq positional selector takes an integer n an finds the nth matching element.

    So if you said td:eq(1) -- you'd get the 2nd TD element in the table (second because the first index is zero/0).

    My guess is that you don't want to use the :contains selector because you're looking for an exact string match and don't want partial matches.

    I'm not aware that jquery has a built in selector that will meet your needs (if so, please correct me). You could add one as an extension or use another method, such as an attribute selector to do the search for you.

    If you are able to control the generated HTML, you could add an ID attribute to each TD like so:

       
    ServerMemory
    Mars4 GB
    Venus1 GB
    Jupiter2 GB
    Uranus8 GB
    Mars_20104 GB

    The following attribute selector would locate the correct TD in the table:

       
       
    

    If you instead want to target the entire row that has the TD that you're looking for, you can add additional selectors:

          $("#servertable tbody tr").css("background-color", "");
          $("#servertable tbody tr:has(td[id='server_" + server.toLowerCase() + "'])").css("background-color", "#FFFF00");
    

    The tbody tag isn't completely necessary, it just helps to distinguish between rows in the table body and rows in the table header.

提交回复
热议问题