如何从具有特定ID

时间:2018-05-01 15:47:37

标签: javascript jquery html

我有两个列表,我想删除所有li标记,但那些包含-all作为其id的输入元素的li标记除外。这是我在jQuery中使用选择器可以实现的吗?可能不使用选择器?

这就是我目前所拥有的:$("#search-filter-column ul li").remove();

<ul>
   <li>
       <input id="filter-types-all" name="filter-types-all" type="checkbox" checked="checked">
   </li>
   <li>
       <input id="filter-types-suv" name="filter-types-suv" type="checkbox" checked="checked">
   </li>
   <li>
       <input id="filter-types-special" name="filter-types-special" type="checkbox" checked="checked">
   </li>
</ul>


<ul>
   <li>
       <input id="filter-company-all" name="filter-company-all" type="checkbox" checked="checked">
   </li>
   <li>
       <input id="filter-company-1" name="filter-company-1" type="checkbox" checked="checked">
   </li>
   <li>
       <input id="filter-company-2" name="filter-company-2" type="checkbox" checked="checked">
   </li>
</ul>


<ul>
   <li>
       <input id="filter-location-1" name="filter-location-1" type="checkbox" checked="checked">
   </li>
   <li>
       <input id="filter-location-2" name="filter-location-2" type="checkbox" checked="checked">
   </li>
   <li>
       <input id="filter-location-3" name="filter-location-3" type="checkbox" checked="checked">
   </li>
</ul>

4 个答案:

答案 0 :(得分:9)

您可以测试input元素内的li元素,然后使用 {{3},而不是搜索input元素然后测试其内容 :not() CSS pseudo-class 以及 "ends-with" attribute selector ($=) 排除任何不相关的li元素。然后,您可以使用 "starts-with" (^=) attribute selector 方法删除所有匹配元素的父// Remove all input elements that are descendants of an <li> that don't have // an id that ends with ($=) "all" or an id that starts with "filter-location-". $("#search-filter-column ul > li input:not([id$='-all']):not([id^='filter-location-'])").parent().remove();

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="search-filter-column">
<ul>
   <li>
     <input id="filter-types-all" name="filter-types-all" type="checkbox" checked="checked">
   </li>
   <li>
     <input id="filter-types-suv" name="filter-types-suv" type="checkbox" checked="checked">
   </li>
   <li>
     <input id="filter-types-special" name="filter-types-special" type="checkbox" checked="checked">
   </li>
</ul>
<ul>
   <li>
     <input id="filter-company-all" name="filter-company-all" type="checkbox" checked="checked">
   </li>
   <li>
     <input id="filter-company-1" name="filter-company-1" type="checkbox" checked="checked">
   </li>
   <li>
     <input id="filter-company-2" name="filter-company-2" type="checkbox" checked="checked">
   </li>
</ul>
<ul>
   <li>
     <input id="filter-location-1" name="filter-location-1" type="checkbox" checked="checked">
   </li>
   <li>
     <input id="filter-location-2" name="filter-location-2" type="checkbox" checked="checked">
   </li>
   <li>
     <input id="filter-location-3" name="filter-location-3" type="checkbox" checked="checked">
   </li>
</ul>
</div>
&#13;
must_not
&#13;
&#13;
&#13;

答案 1 :(得分:3)

是的,您可以使用:not:hasattribute-ends-with selector

$("#search-filter-column ul li:not(:has(input[id$=-all]))").remove();

匹配li ul#search-filter_columninput元素,这些元素不包含id-all结尾的:not

在其他地方的评论中你说过

  

我也有包含-location-的li元素。我也不想删除这些li元素。

您可以拥有多个li:not(this):not(that)条件,*=。它们和CSS选择器的其他方面一样。所以你要attribute contains selector$("#search-filter-column ul li:not(:has(input[id$=-all])):not(:has(input[id*=-location-]))").remove(); )这样做:

$("#search-filter-column ul li:not(:has(input[id$=-all])):not(:has(input[id*=-location-]))").remove();

直播示例:

&#13;
&#13;
<div id="search-filter-column">
  Types:
  <ul>
     <li>
         <input id="filter-types-all" name="filter-types-all" type="checkbox" checked="checked">
     </li>
     <li>
         <input id="filter-types-suv" name="filter-types-all" type="checkbox" checked="checked">
     </li>
     <li>
         <input id="filter-types-special" name="filter-types-all" type="checkbox" checked="checked">
     </li>
  </ul>

  Companies:
  <ul>
     <li>
         <input id="filter-company-all" name="filter-company-all" type="checkbox" checked="checked">
     </li>
     <li>
         <input id="filter-company-1" name="filter-company-all" type="checkbox" checked="checked">
     </li>
     <li>
         <input id="filter-company-2" name="filter-company-all" type="checkbox" checked="checked">
     </li>
  </ul>
  
  Locations:
  <ul>
     <li>
         <input id="filter-location-1" name="filter-location-1" type="checkbox" checked="checked">
     </li>
     <li>
         <input id="filter-location-2" name="filter-location-2" type="checkbox" checked="checked">
     </li>
     <li>
         <input id="filter-location-3" name="filter-location-3" type="checkbox" checked="checked">
     </li>
  </ul>

</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
<android.support.constraint.ConstraintLayout
    android:id="@+id/layout_charts_list"
    tools:context="apppath.fragments.ChartListFragment">

    <TextView
        android:id="@+id/loading_list"
        android:text="@string/loading_my_charts" />

    <TextView
        android:id="@+id/empty_list"
        android:text="@string/my_charts_empty"
        android:visibility="gone"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/charts_list" />

</android.support.constraint.ConstraintLayout>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可以使用jQuery的filter函数来过滤掉您不想删除的元素:

$("#search-filter-column ul li").filter(function() { 
  return $(this).find('input[id$=-all]').length;
}).remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="search-filter-column">
  <ul>
     <li>
         <input id="filter-types-all" name="filter-types-all" type="checkbox" checked="checked">
     </li>
     <li>
         <input id="filter-types-suv" name="filter-types-all" type="checkbox" checked="checked">
     </li>
     <li>
         <input id="filter-types-special" name="filter-types-all" type="checkbox" checked="checked">
     </li>
  </ul>


  <ul>
     <li>
         <input id="filter-company-all" name="filter-company-all" type="checkbox" checked="checked">
     </li>
     <li>
         <input id="filter-company-1" name="filter-company-all" type="checkbox" checked="checked">
     </li>
     <li>
         <input id="filter-company-2" name="filter" type="checkbox" checked="checked">
     </li>
  </ul>
</div>

在您的示例中,我更改了一些过滤器,以便在ID中没有-all,以显示它实际上会删除它们。

答案 3 :(得分:-4)

您尝试按ID选择,获取父级并删除父级。 例如:

$('#ID_REMOVE').parent().remove()