javascript - 删除jquery中没有特定后代元素的所有元素

标签 javascript jquery jquery-selectors

所以我有以下标记:

<div class="festi-cart-products-content">
    <table class="festi-cart-list">
        <tbody>
            <tr class="festi-cart-item ">
                <td class="festi-cart-product-img">
                </td>
                <td class="festi-cart-product-title">
                    <a class="festi-cart-title" href="">product name</a><br>
                    <span class="festi-cart-product-price">
                        <span class="woocommerce-Price-amount amount"></span>
                    </span>
                </td>
            </tr>
        </tbody>
    </table>
</div>

有多个 tr 元素,但只有其中一些具有“woocommerce-Price-amount”类的最内层跨度。另一个在“festi-cart-product-price”分类跨度之后什么都没有。

我正在尝试使用 jQuery 删除其中没有“woocommerce-Price-amount”跨度的所有 tr 元素。

jQuery( document ).ajaxComplete(function() {

    jQuery(".festi-cart-product-price:not(:has(>span))").each(function(){
        jQuery(this).parent('tr.festi-cart.item').hide();
    });

});

我一直在尝试使用 :not 选择器,但它似乎没有产生任何结果。我真的不确定哪里出了问题。

你们中的任何人都可以发现我的代码哪里出了问题,或者它是否是一种完全没有希望的简单解决方案?

最佳答案

使用 .filter功能来匹配具有特定要求的元素。

使用 $('tr').find('.woocommerce-Price-amount').length > 0检查 tr 中是否存在元素.

比简单地做 .hide() (或 .remove() )过滤元素。

$(document).ready(function() {
  var hasWoo = $('.festi-cart-list tr').filter(function() {
    return $(this).find('.woocommerce-Price-amount').length !== 0;
  });

  hasWoo.hide();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="festi-cart-products-content">
  <table class="festi-cart-list">
    <tbody>
      <tr class="festi-cart-item ">
        <td class="festi-cart-product-img">
        </td>
        <td class="festi-cart-product-title">
          <a class="festi-cart-title" href="">product name</a>
          <br>
          <span class="festi-cart-product-price">
                        <span class="woocommerce-Price-amount amount">WOO</span>
          </span>
        </td>
      </tr>
      <tr class="festi-cart-item ">
        <td class="festi-cart-product-img">
        </td>
        <td class="festi-cart-product-title">
          <a class="festi-cart-title" href="">product name</a>
          <br>
          <span class="festi-cart-product-price">
                        <span class="woocommerce-Price-amount amount">WOO</span>
          </span>
        </td>
      </tr>
      <tr class="festi-cart-item ">
        <td class="festi-cart-product-img">
        </td>
        <td class="festi-cart-product-title">
          <a class="festi-cart-title" href="">product name</a>
          <br>
          <span class="festi-cart-product-price">
                      EMPTY
                    </span>
        </td>
      </tr>
    </tbody>
  </table>
</div>

关于javascript - 删除jquery中没有特定后代元素的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40504881/

相关文章:

jquery - 如何和某个类(class)的家长一起选择当前元素?

javascript - 如何只打开新标签页 1 次?

jquery - CORS post 不适用于 Rails 应用

javascript - 在toggleClass之后ClassName变得未定义

javascript - 点击网页中其他内容时如何关闭导航栏

dom - jQuery .each() 返回 DOM 元素而不是 jQuery 对象

javascript - 使用 JQuery 生成 HTML,然后使用 .html() 对其进行更改。在 FF 中可以,但在 Chrome 中不行?

javascript - Bcrypt.compareSync 总是返回 false

javascript - 如何根据子属性将 JSON 排序到数组中

javascript - 使用动态 HTML 导入在模板中标记时, polymer 元素的尺寸会损坏