首页 > 解决方案 > jquery单击内容时如何从内容中删除类

问题描述

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/

标签: jqueryclick

解决方案


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

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

推荐阅读