dongzi0850 2012-08-19 05:12
浏览 15
已采纳

检查div元素列表中是否存在children元素,并根据该元素将类添加到另一个元素

Wondering if somebody could help me figuring this out, I have a list; basically I want jQuery to check if inside every div with class "bullet-btn" a children element has a class "icon-thumbs-up" if so find another element inside the same div with a class "badge" and change color of it.

Basically the way I have the script is not filtering the all list and it is selecting all the element with class "icon-thumbs-up" any ideas?

Here is my html

<ul class="content-box">
<li>
    <div class="bullet-btn">
    <h1>Hamburger</h1>
    <span class="counter badge">389</span>
    <span class="icon-thumbs-up"></span>
    </div>
    <div class="bullet-btn">
    <h1>Sirloin</h1>
    <span class="counter badge">389</span>
    <span class="icon-thumbs-down"></span>
    </div>
    <a href="#">View All Trending Topics...</a> 
</li>
<li>
    <div class="bullet-btn">
    <h1>Brett</h1>
    <span class="counter badge">389</span>
    <span class="icon-thumbs-down"></span>
    </div>
    <div class="bullet-btn">
    <h1>Jenny</h1>
    <span class="counter badge">389</span>
    <span class="icon-thumbs-up"></span>
    </div>
    <a href="#">View All Trending Topics...</a> 
</li>
<li>
    <div class="bullet-btn">
    <h1>Lighting</h1>
    <span class="counter badge">389</span>
    <span class="icon-thumbs-up"></span>
    </div>
    <div class="bullet-btn">
    <h1>Music</h1>
    <span class="counter badge">389</span>
    <span class="icon-thumbs-down"></span>
    </div>
    <a href="#">View All Trending Topics...</a> 
</li>
</ul>

Here is my jQuery

     if ($('div.bullet-btn').children('.icon-thumbs-down').length > 0) {
    $(this).find(".badge").css({'color':'red'});
} else {
    $(this).find(".badge").css({'color':'white'});
}
  • 写回答

1条回答 默认 最新

  • dongwei1954 2012-08-19 05:16
    关注
    $('.bullet-btn').each(function() {
        var color = $('.icon-thumbs-down', this).length ? 'red' : 'white';
        $('.badge', this).css('color', color);
    });
    

    Here's the fiddle: http://jsfiddle.net/PSSWc/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器