如何构建具有多个点击值的点击计数器并将它们组合成总点击值?

时间:2016-10-19 17:15:57

标签: javascript jquery

我是一个新手,我用jquery构建了这个点击计数器,它工作得很好,但对我来说似乎很笨重,我想让我的代码更干净。所以我的问题是如何将两个单击函数组合成一个函数,然后使用两个变量的总和" click1"和" click2"作为变量的值" sum"?我假设一个人会遍历单独的元素,并将他们的点击添加到一个数组,然后添加存储在数组中的值,并将其作为总数分配,但我不知道从哪里开始,或者甚至可能。我正在学习jquery和普通的javascript,所以我可以理解任何语法中的建议。谢谢大家!

CODEPEN:2

<div class="clickButton">
    <button id="updateClick1" type="button">click me</button>
    <span>Number of Clicks: <span class="clickNumber">0</span></span>
</div>

<div class="clickButton">
    <button id="updateClick2" type="button">click me</button>
    <span>Number of Clicks: <span class="clickNumber">0</span></span>
</div>

<div>Total Clicks: <span id="sumClicks">0</span></div>

<script>
    var count1 = 0;
    var count2 = 0;
    var sum = 0;

    $('#updateClick1').click(function() {
      count1++;
      sum++;
      $(this).siblings().children('.clickNumber').html(count1);
      $('#sumClicks').html(sum);
    });

    $('#updateClick2').click(function() {
      count2++;
      sum++;
      $(this).siblings().children('.clickNumber').html(count2);
      $('#sumClicks').html(sum);
    });
</script>

3 个答案:

答案 0 :(得分:0)

您可以这样做:

RewriteCond %{HTTP_HOST} ^domain1\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain1\.com$
RewriteCond %{REQUEST_URI} !^/(admin) [NC]  
RewriteRule (.*) http://www.domain2.com [R=301,L]

这是codepen: sum of clicks

答案 1 :(得分:0)

检查以下代码段

&#13;
&#13;
var count=0;
var sum = 0;
var $sumElement=$('#sumClicks');
sum=parseInt($sumElement.html());
$(document).ready(function(){
$('button').click(function() {
  var element=$(this);
  var $clickCount=$(this).siblings().children('.clickNumber');
 
  var clickCountVal=parseInt($clickCount.html());
 clickCountVal++;
  sum++;
  $clickCount.html(clickCountVal);
 $sumElement.html(sum);
});
  });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="clickButton">
  <button id="updateClick1" type="button">click me</button>
  <span>Number of Clicks: <span class="clickNumber">0</span></span>
</div>

<div class="clickButton">
  <button id="updateClick2" type="button">click me</button>
  <span>Number of Clicks: <span class="clickNumber">0</span></span>
</div>

<div>Total Clicks: <span id="sumClicks">0</span></div>
&#13;
&#13;
&#13;

希望这有帮助

答案 2 :(得分:0)

你可以试试这样的东西,它会更清洁一点:

<div class="clickButton">
    <button id="updateClick1" type="button">click me</button>
    <span>Number of Clicks: <span class="clickNumber1">0</span></span>
</div>

<div class="clickButton">
   <button id="updateClick2" type="button">click me</button>
   <span>Number of Clicks: <span class="clickNumber2">0</span></span>
</div>

<div>Total Clicks: <span id="sumClicks">0</span></div>



var count = [0,0];

$('#updateClick1').click(function() {
  count[0]++;
  $('.clickNumber1').html(count[0]);
  $('#sumClicks').html(count[0] + count[1]);
});

$('#updateClick2').click(function() {
  count[1]++;
  $('.clickNumber2').html(count[1]);
  $('#sumClicks').html(count[0] + count[1]);
});

JSFIDDLE:https://jsfiddle.net/s7pw5oqs/1/