使用jquery找到最后2个孩子

时间:2015-08-24 12:51:13

标签: javascript jquery html

我的HTML就像这样



<div class="accordion_example2 smk_accordion acc_with_icon"> 
    <div class="accordion_in">
        <div class="acc_head accordian-title-white">
            <div class="acc_icon_expand"></div>
            19714320
        </div>
        <div class="acc_content accordian-content-white" style="display: none;">
            <ul>
             
                <li>
                    Break Load (kg) 6.05
                </li>
            </ul>
        </div>
    </div>
    <div class="accordion_in">
        <div class="acc_head accordian-title-blue">
            <div class="acc_icon_expand"></div>
            mere test tekst …
        </div>
        <div class="acc_content accordian-content" style="display: none;">
            <ul>
              
               
                <li>
                    Break Load (kg)
                </li>
            </ul>
        </div>
    </div>
    <div class="accordion_in">
        <div class="acc_head accordian-title-white">
            <div class="acc_icon_expand"></div>
            test tekst
        </div>
        <div class="acc_content accordian-content-white" style="display: none;">
            <ul>
               
                <li>
                    Break Load (kg)
                </li>
            </ul>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;

我想查找最后两个accordion_in元素并从中删除其子元素accordian-content。我怎样才能通过Jquery实现这一目标? 我的预期结果是这样的

&#13;
&#13;
<div class="accordion_example2 smk_accordion acc_with_icon"> 
    <div class="accordion_in">
        <div class="acc_head accordian-title-white">
            <div class="acc_icon_expand"></div>
            19714320
        </div>
        <div class="acc_content accordian-content-white" style="display: none;">
            <ul>
             
                <li>
                    Break Load (kg) 6.05
                </li>
            </ul>
        </div>
    </div>
    <div class="accordion_in">
        <div class="acc_head accordian-title-blue">
            <div class="acc_icon_expand"></div>
            mere test tekst …
        </div>
        
    </div>
    <div class="accordion_in">
        <div class="acc_head accordian-title-white">
            <div class="acc_icon_expand"></div>
            test tekst
        </div>
        
    </div>
</div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:3)

试试这个:您可以使用.slice()获取最后两个元素,然后从中移除accordian-content div。

&#13;
&#13;
$(function(){
  $('div.accordion_example2 .accordion_in').slice("-2").find(".accordian-content").remove();
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="accordion_example2 smk_accordion acc_with_icon"> 
    <div class="accordion_in">
        <div class="acc_head accordian-title-white">
            <div class="acc_icon_expand"></div>
            19714320
        </div>
        <div class="acc_content accordian-content-white" style="display: none;">
            <ul>
             
                <li>
                    Break Load (kg) 6.05
                </li>
            </ul>
        </div>
    </div>
    <div class="accordion_in">
        <div class="acc_head accordian-title-blue">
            <div class="acc_icon_expand"></div>
            mere test tekst …
        </div>
        <div class="acc_content accordian-content" style="display: none;">
            <ul>
              
               
                <li>
                    Break Load (kg)
                </li>
            </ul>
        </div>
    </div>
    <div class="accordion_in">
        <div class="acc_head accordian-title-white">
            <div class="acc_icon_expand"></div>
            test tekst
        </div>
        <div class="acc_content accordian-content-white" style="display: none;">
            <ul>
               
                <li>
                    Break Load (kg)
                </li>
            </ul>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

可以使用:gt()选择器,并使用负值,它将从最后开始工作。需要jQuery 1.8 +

$('.accordion_in:gt(-3)').find('.acc-content').remove();

参考::gt() selector docs

答案 2 :(得分:-1)

您可以使用de :nth-last-of-type选择器

执行此操作

$(".class:nth-last-of-type(-n+2)")选择最后2个.class元素。

在你的情况下:

$(".accordion_in:nth-last-of-type(-n+2)").children(".accordian-content").remove();

或者使用find:     $( “accordion_in:第n-最后的型(-n + 2) ”)。发现(“。手风琴内容”)除去();

修改:必须是-n + 2而不是2。