简体   繁体   English

缺少针对遍历 dom 的正确元素

[英]missing to target the correct element traversing up the dom

I have a HTML structure like the following (relevant part):我有一个 HTML 结构,如下所示(相关部分):

<div class="col-md-6">
    <div class="card card-warning">something here</div>
</div>
<div class="col-md-6">
    <div class="card">something else here</div>
</div>

I want to be able to remove the class card-warning from the first card and add it to the second on toggle of a radio button (one in each card).我希望能够从第一张卡中删除 class card-warning ,并将其添加到第二张单选按钮的切换按钮(每张卡中一个)。

The code I have so far adds correctly the class to the second card but fails to remove it from the first.到目前为止,我的代码将 class 正确添加到第二张卡,但无法从第一张卡中删除。

$(this).closest('div.card').addClass('card-warning').prev('div.card').removeClass('card-warning');

$(this) is the selected radio button and .closest('div.card').addClass('card-warning') correctly adds the class as expected. $(this)是选中的单选按钮, .closest('div.card').addClass('card-warning')按预期正确添加了 class。 The point is that .prev('div.card').removeClass('card-warning') is not targeting the previous card.关键是.prev('div.card').removeClass('card-warning')不是针对前一张卡。

What am I doing wrong?我究竟做错了什么?

The closest div.card you're selecting does not have any previous siblings, so the .prev fails.您选择的最近的div.card没有任何先前的兄弟姐妹,因此.prev失败。 Navigate to the previous element of the parent element instead:改为导航到元素的前一个元素:

$(this)
  .closest('div.card')
  .addClass('card-warning')
  .parent()
  .prev()
  .children('div.card')
  .removeClass('card-warning');

 $('#foo').on('click', function() { $(this).closest('div.card').addClass('card-warning').parent().prev().children('div.card').removeClass('card-warning'); });
 .card-warning { background-color: yellow; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="col-md-6"> <div class="card card-warning">something here</div> </div> <div class="col-md-6"> <div class="card" id="foo">something else here</div> </div>

If you'll only have one .card-warning in the DOM at any given time, instead of dynamically navigating to parents/siblings etc, just select the one element with .card-warning in the DOM first:如果您在任何给定时间在 DOM 中只有一个.card-warning ,而不是动态导航到父母/兄弟姐妹等,则只需 select 首先在 DOM 中具有.card-warning的一个元素:

$('.card-warning').removeClass('card-warning');
$(this)
  .closest('div.card')
  .addClass('card-warning');

I dont know if this what you you search but below snippet, show example我不知道这是否是您搜索的内容,但在代码段下方,显示示例

  • first add class to closest div card首先将 class 添加到最近的 div 卡

  • then remove class from other div.card except current using然后从其他 div.card 中删除 class 除了当前使用

    .parents('.row').find('div.card').not($(this).parents('div.card')) .parents('.row').find('div.card').not($(this).parents('div.card'))

 $(function() { $(".apply").on("click", function() { $(this).closest('div.card').addClass('card-warning').parents('.row').find('div.card').not($(this).parents('div.card')).removeClass("card-warning"); }) })
 .apply { margin-left:20px; color: red; }.card-warning { background-color: yellow;important; }
 <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="container"> <div class="row"> <div class="col-md-6"> <div class="card card-warning">something here <input name="apply" class="apply" type="radio"/></div> </div> <div class="col-md-6"> <div class="card">something else here <input name="apply" class="apply" type="radio"/></div> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM