【问题标题】:Multiple fa-heart like count increment and decrement多个fa-heart,如计数递增和递减
【发布时间】:2018-08-30 14:13:40
【问题描述】:

我需要在点击时增加 fa-heart 值的计数,在第二次点击时减少。

问题是我在同一页面中有多个 fa-heart,因此无法对单击的 fa-heart 进行递增/递减。

下面是我的小提琴

https://jsfiddle.net/mehbub/d9e2qotg/1/

(function() {
  const heart = document.getElementById('heart');
  heart.addEventListener('click', function() {
    heart.classList.toggle('red');
  });
})();

$(document).on('click', ".notliked", function() {
    var $this = $(this);
    $this.removeClass('notliked');
    $this.addClass('liked')
    $count = $('.likes-count');
    $count.text(function(idx, txt) {
    return (+txt == 0) ? 0 : (+txt - 1);
    heart.classList.toggle('grey');

    });

});

$(document).on('click', ".liked", function() {
    var $this = $(this);
    $this.removeClass('liked');
    $this.addClass('notliked');
    $count = $('.likes-count');
    $count.text(function(idx, txt) {
            return +txt + 1;
    heart.classList.toggle('red');

    });

});
$count.text(function(idx, txt) {
   // convert text to number and increment by one
   return +txt + 1;
});
#heart {
  color: grey;  
  font-size: 20px;
}

#heart.red {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<p>
robert stephen</p><i class="fa fa-heart liked heart" id="heart" value="1" ></i>
<span class="likes-count"> 100 </span><br>
<p>
James Camroon</p>
<i class="fa fa-heart liked heart" id="heart" value="1" ></i>
<span class="likes-count"> 101 </span><br>

<p>
John Wick</p>
<i class="fa fa-heart liked heart" id="heart" value="1" ></i>
<span class="likes-count"> 37 </span><br>

<p>
James Bond</p>
<i class="fa fa-heart liked heart" id="heart" value="1" ></i>
<span class="likes-count"> 22 </span><br>

<p>
Avengers</p>
<i class="fa fa-heart liked heart" id="heart" value="1" ></i>
<span class="likes-count"> 90 </span>

我需要为每个 fa-heart 单独单击增量/减量,以在增量时将颜色变为红色,如果减量则变为灰色

谢谢

【问题讨论】:

  • 文档的 ID 必须是唯一的。在每颗心上使用唯一的 id。此外,在您的事件处理程序中,获取对单击的元素的引用,而不是使用全局变量,例如,使用 $(this)
  • 但是如果我为动态数据做,我会循环通过for循环..那怎么做呢?

标签: javascript jquery html css onclick


【解决方案1】:

您应该删除重复的 id heart 或将其删除,因为您已经有一个类 heart 然后将点击附加到这个公共类并切换类 liked/notliked,您可以将这些类用作规则在您的 CSS 中,因此不需要 red/grey 类,例如:

$(document).on('click', ".heart", function() {
  var count = $(this).next('span');

  $(this).toggleClass('notliked liked');

  if ($(this).hasClass('liked')) {
    count.text(Number(count.text()) + 1);
  } else {
    count.text(Number(count.text()) - 1);
  }
});
.heart.notliked {
  color: grey;
  font-size: 20px;
}

.heart.liked {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<p>robert stephen</p>
<i class="fa fa-heart liked heart" value="1"></i>
<span class="likes-count"> 100 </span><br>

<p>James Camroon</p>
<i class="fa fa-heart liked heart" value="1"></i>
<span class="likes-count"> 101 </span><br>

<p>John Wick</p>
<i class="fa fa-heart liked heart" value="1"></i>
<span class="likes-count"> 37 </span><br>

<p>James Bond</p>
<i class="fa fa-heart liked heart" value="1"></i>
<span class="likes-count"> 22 </span><br>

<p>Avengers</p>
<i class="fa fa-heart liked heart" value="1"></i>
<span class="likes-count"> 90 </span>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 1970-01-01
    相关资源
    最近更新 更多