Javascript 文本标记按钮(JQuery)内的递增计数器

Javascript 文本标记按钮(JQuery)内的递增计数器,javascript,jquery,scope,Javascript,Jquery,Scope,我想分别增加每个按钮的计数。我知道.find jquery函数会派上用场。但是,我的按钮不是递增的 i、 e。 “你点击我:100次” “你点击我:22次” “你点击我:71次” $('#target')。在('click',function()上{ var numotimes=parseInt($(this.find('span').html()); $(this.find('span').html(numoftimes+1); }); 你点击我:0次 你点击我:0次 您单击我:0次您需要

我想分别增加每个按钮的计数。我知道.find jquery函数会派上用场。但是,我的按钮不是递增的

i、 e。 “你点击我:100次” “你点击我:22次” “你点击我:71次”

$('#target')。在('click',function()上{
var numotimes=parseInt($(this.find('span').html());
$(this.find('span').html(numoftimes+1);
});

你点击我:0次
你点击我:0次

您单击我:0次
您需要使用类而不是ID。ID需要唯一

$('.target')。在('click',function()上{
var numotimes=parseInt($(this).find('span').html());//或.text()
$(this.find('span').html(numoftimes+1);//或.text()
});

你点击我:0次
你点击我:0次

您单击我:0次
您需要使用类而不是ID。ID需要唯一

$('.target')。在('click',function()上{
var numotimes=parseInt($(this).find('span').html());//或.text()
$(this.find('span').html(numoftimes+1);//或.text()
});

你点击我:0次
你点击我:0次
您单击我:0次
属性id在文档中应该是唯一的,请改用类。此外,我建议您在处理纯文本内容时使用
.text()
而不是
.html()

$('.target')。在('click',function()上{
var numoftimes=parseInt($(this.find('span').text());
$(this.find('span').text(numoftimes+1);
});

你点击我:0次
你点击我:0次
您单击我:0次
属性id在文档中应该是唯一的,请改用类。此外,我建议您在处理纯文本内容时使用
.text()
而不是
.html()

$('.target')。在('click',function()上{
var numoftimes=parseInt($(this.find('span').text());
$(this.find('span').text(numoftimes+1);
});

你点击我:0次
你点击我:0次

您单击我:0次
您使用的id对于每个元素都必须是唯一的,这样您就可以定义可以是公共的并且可以用于相同元素的类

$('.target').on('click', function() {
  var numoftimes = parseInt($(this).find('span').html());
  $(this).find('span').html(numoftimes + 1);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="target" class="target" type="button">You clicked me: <span>0</span>times</button>
<button id="target1" class="target" type="button">You clicked me: <span>0</span>times</button>
<button id="target2" class="target" type="button">You clicked me: <span>0</span>times</button>
$('.target')。在('click',function()上{
var numotimes=parseInt($(this.find('span').html());
$(this.find('span').html(numoftimes+1);
});
你点击我:0次
你点击我:0次
你点击我:0次

您使用的id对于每个元素都必须是唯一的,这样您就可以定义可以是公共的并且可以用于相同元素的类

$('.target').on('click', function() {
  var numoftimes = parseInt($(this).find('span').html());
  $(this).find('span').html(numoftimes + 1);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="target" class="target" type="button">You clicked me: <span>0</span>times</button>
<button id="target1" class="target" type="button">You clicked me: <span>0</span>times</button>
<button id="target2" class="target" type="button">You clicked me: <span>0</span>times</button>
$('.target')。在('click',function()上{
var numotimes=parseInt($(this.find('span').html());
$(this.find('span').html(numoftimes+1);
});
你点击我:0次
你点击我:0次
你点击我:0次

应该知道是.text vs.html扫描让我失望了。正是我想要的。万分感谢@NanoNet,非常欢迎:)应该知道是.text vs.html扫描让我扫兴。正是我想要的。万分感谢@NanoNet,非常欢迎您:)