简体   繁体   中英

JQuery select tr when all images in td are not example.png

How do i select those tr's which all DONT have the example.png in one of their td's? Every image has an anchor, so i thought i may select by that... i never worked with jquery before and its quite confusing^^

$(document).ready(function(){
    $("td > a").parent().parent().css("background-color","lime");
});

Here one of my tr's but with only the first anchor/image (it is already too long):

<tr bgcolor='$color'>
    <td><a href='request.php?id=$requestid'>#$requestid [show request]</a></td>
    <td>$generation&nbsp;&nbsp;</td>
    <td>$custname&nbsp;&nbsp;</td>
    <td>$platform&nbsp;&nbsp;</td>
    <td>$phase&nbsp;&nbsp;</td>
    <td>$tmc&nbsp;&nbsp;</td>
    <td>$pcstroke&nbsp;&nbsp;</td>
    <td>$scstroke&nbsp;&nbsp;</td>
    <td>
        <a href='myurl.php?selection=" . $flowid."' title='new'>
            <img align='center' src='images/new.png'
                 alt='' style='width:20px; height:20px;' />
        </a>&nbsp;&nbsp;
    </td>
    $status
    <td><b>$prelease</b></td>
</tr>;

Thanks in advance.

You can use not and has

 var allTrs = $("tbody tr"), nonExample = allTrs.not(':has(img[src*="example.jpg"])'); nonExample.css("background-color", "red"); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tbody> <tr><td>foo</td><td><img src="foo.jpg" alt="foo"/></td></tr> <tr><td>example</td><td><img src="example.jpg" alt="example"/></td></tr> <tr><td>bar</td><td><img src="bar.jpg" alt="bar"/></td></tr> <tr><td>example</td><td><img src="example.jpg" alt="example"/></td></tr> </tbody> </table> 

Select table's existing img , and then parent 3 times, as they are expected to be located within anchors tr>td>a>img :

$(document).ready(function(){
    $images = $('table tr>td>a>img'); // Choose the right table selector
    $rows = $images.closest('tr');
    console.log($rows);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM