gpt4 book ai didi

javascript - 使用 jquery 为具有特定类的每个父类选择第一个没有特定类的直接子类

转载 作者:行者123 更新时间:2023-11-29 17:07:33 25 4
gpt4 key购买 nike

我想创建一个脚本,它将为第一个没有特定类的 child 添加一个类,为每个具有特定类的父类添加一个类。例如:

<div class="wrap">
<p class="no">1. Not this one please</p>
<div>
<p>2. not this either</p>
</div>
<p>3. Make this red!</p>
<p>4. Not this</p>
</div>
<div class="wrap">
<p class="no">5. Not this one please</p>
<p>6. Make this red also!</p>
<p>7. Not this</p>
</div>

我的javascript如下:

$('div.wrap').find('p:not(.no):first').addClass('red');

这将选择没有 class="no" 的后代,而不是第一个 child (因此是第 2 项,而不是上例中的第 3 项)。我试过将 .find 更改为 .children,但它只会选择页面上的第一个示例,而不是每组中的第一个(因此只有项目 2以上示例,不是第 6 项)。

这是一个代码笔,说明了上述 http://cdpn.io/izGDH

最佳答案

你很亲近!你想添加一个指令来只抓取直接后代:“>”

$('div.wrap').find('> p:not(.no):first').addClass('red');

http://jsfiddle.net/wnewby/HUqR9/

关于javascript - 使用 jquery 为具有特定类的每个父类选择第一个没有特定类的直接子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23436422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com