jquery如何在单击内容时从内容中删除类

问题描述 投票:0回答:1

单击图标时,我想从心脏图标中删除类text-danger,但我没有成功完成:

$(document).ready(function(){
  $('.blog-like-counter').on('click', function (e) {
    e.preventDefault();
    $(this).next('.heart-icon').removeClass('text-danger'); // remove red color heart icon  
  });
});

<div class="blog-like-counter">
    <p class="heart-icon text-danger">&#9829;</p>
</div>

https://jsfiddle.net/cp8ms2tu/1/

jquery click
1个回答
1
投票

请使用find方法,因为next方法获取的是下一个元素而不是内部元素

      $('.blog-like-counter').on('click', function (e) {
        e.preventDefault();
        $(this).find('.heart-icon').removeClass('text-danger'); // remove red color heart icon
      });
© www.soinside.com 2019 - 2024. All rights reserved.