javascript - 将类似的类与 jQuery .first() 结合起来

标签 javascript jquery html

我试图删除多个 div 之间的公共(public)类,但只针对每个 div 中的第一个 p 以删除该类名。

这是我的 HTML:

<div class="items">
  <div class="wrapper">
    <div class="thing thing--one">
      <h2>This title</h2>
      <p class="extended-content">A good act does not wash out the bad, nor a bad act the good. Each should have its own reward. Words are like wind. And now his watch is ended. Bastards are born of passion, aren't they? We don't despise them in Dorne. King in the North.</p>
      <p class="extended-content">All men must die. It's ten thousand miles between Kings landing and the wall. A good act does not wash out the bad, nor a bad act the good. Each should have its own reward. The wolf and the lion. All men must die.</p>
    </div>
    <div class="thing thing--two">
      <h2>This title</h2>
      <p class="extended-content">A good act does not wash out the bad, nor a bad act the good. Each should have its own reward. Words are like wind. And now his watch is ended. Bastards are born of passion, aren't they? We don't despise them in Dorne. King in the North.</p>
      <p class="extended-content">All men must die. It's ten thousand miles between Kings landing and the wall. A good act does not wash out the bad, nor a bad act the good. Each should have its own reward. The wolf and the lion. All men must die.</p>
    </div>
    <div class="thing thing--three">
      <h2>This title</h2>
      <p class="extended-content">A good act does not wash out the bad, nor a bad act the good. Each should have its own reward. Words are like wind. And now his watch is ended. Bastards are born of passion, aren't they? We don't despise them in Dorne. King in the North.</p>
      <p class="extended-content">All men must die. It's ten thousand miles between Kings landing and the wall. A good act does not wash out the bad, nor a bad act the good. Each should have its own reward. The wolf and the lion. All men must die.</p>
    </div>
  </div>
</div>

当我这样做时:

$(".thing--one p, .thing--two p, .thing--three p").first().removeClass("extended-content");

它只从 thing--one 类中的第一个 p 中删除第一个 extended-content 类。

所以,目前我是这样写的:

$(".thing--one p").first().removeClass("extended-content");
$(".thing--two p").first().removeClass("extended-content");
$(".thing--three p").first().removeClass("extended-content");

这行得通,但感觉和看起来真的很臃肿,并不是删除这三个 div 中的公共(public)类的最佳方法。

关于如何使它更具语义性的任何想法?

最佳答案

希望对你有帮助

$(".thing").each(function(i) {
  $(this).find("p").first().removeClass("extended-content");
});
.extended-content {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="items">
  <div class="wrapper">
    <div class="thing thing--one">
      <h2>This title</h2>
      <p class="extended-content">A good act does not wash out the bad, nor a bad act the good. Each should have its own reward. Words are like wind. And now his watch is ended. Bastards are born of passion, aren't they? We don't despise them in Dorne. King in the North.</p>
      <p class="extended-content">All men must die. It's ten thousand miles between Kings landing and the wall. A good act does not wash out the bad, nor a bad act the good. Each should have its own reward. The wolf and the lion. All men must die.</p>
    </div>
    <div class="thing thing--two">
      <h2>This title</h2>
      <p class="extended-content">A good act does not wash out the bad, nor a bad act the good. Each should have its own reward. Words are like wind. And now his watch is ended. Bastards are born of passion, aren't they? We don't despise them in Dorne. King in the North.</p>
      <p class="extended-content">All men must die. It's ten thousand miles between Kings landing and the wall. A good act does not wash out the bad, nor a bad act the good. Each should have its own reward. The wolf and the lion. All men must die.</p>
    </div>
    <div class="thing thing--three">
      <h2>This title</h2>
      <p class="extended-content">A good act does not wash out the bad, nor a bad act the good. Each should have its own reward. Words are like wind. And now his watch is ended. Bastards are born of passion, aren't they? We don't despise them in Dorne. King in the North.</p>
      <p class="extended-content">All men must die. It's ten thousand miles between Kings landing and the wall. A good act does not wash out the bad, nor a bad act the good. Each should have its own reward. The wolf and the lion. All men must die.</p>
    </div>
  </div>
</div>

关于javascript - 将类似的类与 jQuery .first() 结合起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52501314/

相关文章:

javascript - 我应该为 &lt;textarea&gt; 使用 .innerHTML 还是 .value?

javascript - 修改 input type=number 使其两边都有箭头

javascript - 使用纯 JavaScript 刷新我的井字游戏的最佳方式?

javascript - Div 以光标为中心

javascript - div 元素中的 childNode

jquery - 验证不带 www 的 url。或 http ://

javascript - 使用单个 HTML 文档添加多个页面

javascript - Magento CheckOut js 验证错误

javascript - ReactJS 改变子类的另一个类的状态

javascript - 如何为 FullCalendar 上的 'editable' 属性创建切换按钮