附加几次

时间:2015-01-25 17:53:15

标签: jquery html

我有以下代码多次附加到我的页面:

<li>
  <div class="view">
         <i class="up"></i>
         <div class="score"><p>0</p></div>
  </div>
</li>

我希望在&#34; up&#34;时将分数从0更改为1。仅针对包含&#34; up&#34;的视图单击我点击了。

由于这是附加的div,我无法使用$('.up').click(...

但是,如果我使用:

$(document).on('click', '.up', function(){ 
     $('.score').html("<p>1</p>");
});

然后,这会改变我所有观看次数的分数,而不仅仅是我点击的那个。

通常情况下,我会使用$(this).html() ....但由于我需要$(document).on(),因此无效。

希望这是有道理的,谢谢!

1 个答案:

答案 0 :(得分:2)

您需要使用this,它引用调用事件处理程序的元素。有不同的方法可以遍历元素,例如.next()

$(document).on('click', '.up', function(){ 
     $(this).next('.score').find('p').html("1");
});

OR

$(document).on('click', '.up', function(){ 
     $(this).closest('.view').find('.score p').html("1");
});