简体   繁体   English

通过JQuery选择第一个和最后一个孩子

[英]Pick First and Last Child by JQuery

I have a piece of code as below :- 我有一段代码如下:

<table class="abc">
    <tbody>
        <tr>
            <td id="scriptWPQ22">
                <table id="Hero-WPQ22" dir="none" cellpadding="0" cellspacing="0" border="0" summary="MeetTheTeam" class="greenroom-meettheteam">
                    <tbody>
                        <tr>
                            <td class="ms-list-addnew ms-textLarge ms-soften"></td>
                        </tr>
                    </tbody>
                </table>
                <div></div>
                <iframe src="javascript:false;" id="FilterIframe49" name="FilterIframe49" style="display:none" height="0" width="0" filterlink="?"></iframe>
                <div class="local-intranets">
                    <div class="local-intranets-title">Budapest
                            </div>
                </div>
                <div class="local-intranets">
                    <div class="local-intranets-title">Moscow</div>
                </div>
                <div class="local-intranets">
                    <div class="local-intranets-title">Stockholm
                                    </div>
                </div>
                <div class="local-intranets">
                    <div class="local-intranets-title">Warsaw
                                        </div>
                </div>
            </td>
        </tr>
    </tbody>
</table>

I have to pick first and last local-intranets class by jquery. 我必须通过jquery选择第一个和最后一个本地内联网类。 How to do that. 怎么做。 I have no any other specific class of div to pick. 我没有其他特定的div类可供选择。 I tried first:child and last:child, but its not working. 我尝试了first:child和last:child,但是它不起作用。

You can use the css pseudo-class child selector: 您可以使用css伪类子选择器:

$('.local-intranets:first-child')
$('.local-intranets:last-child')

You can also be more specific: 您还可以更具体:

$('#scriptWPQ22 .local-intranets:first-child')
$('#scriptWPQ22 .local-intranets:last-child')

Or use the jquery first and last methods: 或使用jquery的firstlast方法:

$('.local-intranets').first()
$('.local-intranets').last()

Use first and last methods of jquery 使用jQuery的firstlast方法

var $localIntranets = $( ".local-intranets" );
var first = $localIntranets.first();
var last  = $localIntranets.last();

Edit 编辑

You can use :first-child and :last-child as well. 您也可以使用:first-child:last-child

var first = $( ".local-intranets:first-child" );
var last  = $( ".local-intranets:last-child" );

use Pseudo selector 使用伪选择器

$( ".local-intranets:first" ). $(“ .local-intranets:first”)。

More info 更多信息

  1. One option is using .first() and .last() 一种选择是使用.first() and .last()

 $('.local-intranets').first().css('color','red') $('.local-intranets').last().css('color','red') 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="abc"> <tbody> <tr> <td id="scriptWPQ22"> <table id="Hero-WPQ22" dir="none" cellpadding="0" cellspacing="0" border="0" summary="MeetTheTeam" class="greenroom-meettheteam"> <tbody> <tr> <td class="ms-list-addnew ms-textLarge ms-soften"></td> </tr> </tbody> </table> <div></div> <iframe src="javascript:false;" id="FilterIframe49" name="FilterIframe49" style="display:none" height="0" width="0" filterlink="?"></iframe> <div class="local-intranets"> <div class="local-intranets-title">Budapest </div> </div> <div class="local-intranets"> <div class="local-intranets-title">Moscow</div> </div> <div class="local-intranets"> <div class="local-intranets-title">Stockholm </div> </div> <div class="local-intranets"> <div class="local-intranets-title">Warsaw </div> </div> </td> </tr> </tbody> </table> 

Using filter() and :first and :last selectors 使用filter():first:last选择器

 $('.local-intranets').filter(':first, :last').css('color','red') 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="abc"> <tbody> <tr> <td id="scriptWPQ22"> <table id="Hero-WPQ22" dir="none" cellpadding="0" cellspacing="0" border="0" summary="MeetTheTeam" class="greenroom-meettheteam"> <tbody> <tr> <td class="ms-list-addnew ms-textLarge ms-soften"></td> </tr> </tbody> </table> <div></div> <iframe src="javascript:false;" id="FilterIframe49" name="FilterIframe49" style="display:none" height="0" width="0" filterlink="?"></iframe> <div class="local-intranets"> <div class="local-intranets-title">Budapest </div> </div> <div class="local-intranets"> <div class="local-intranets-title">Moscow</div> </div> <div class="local-intranets"> <div class="local-intranets-title">Stockholm </div> </div> <div class="local-intranets"> <div class="local-intranets-title">Warsaw </div> </div> </td> </tr> </tbody> </table> 

you can use last() or first() to do that. 您可以使用last()first()来做到这一点。 for example to change the color of first and last element of local-intranets class you can do this: 例如,要更改local-intranets类的第一个和最后一个元素的颜色,可以执行以下操作:

$( ".local-intranets" ).last().css( "color", "red" );
$( ".local-intranets" ).first().css( "color", "red" );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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