jQuery为同一个类创建一个click函数

时间:2012-08-08 02:18:39

标签: jquery

例如,如果我想在css类“more-link”的点击上创建一个jQuery函数,但是在页面上有多个“more-link”实例,我怎么能强制jQuery只运行单击的类上的动画,而不是其他类。

2 个答案:

答案 0 :(得分:2)

$('element').click(function(){
  $(this).animate({
  //scope is ONLY the element clicked
  });
  $('element').animate({
    //this wi ll animate ALL elements matching the selector. Not what you want.
  });
});

For anyone who finds this answer, the author actually wanted this.

$('.read-more').click(function(){
  var me = $(this);
  $(this).prev('p').animate({
     height: '100%'
  }, function(){
     //lost $(this) scope...me still holds it, we use it below for a reason.
     $(me).fadeOut(400);
     $(me).next('.hide_post-content').delay(400).fadeIn();
  });  
});    

答案 1 :(得分:0)

在jQuery中,$(this)引用了被点击的元素:

$('.more-link').on('click', function() {
  $(this).animate(...);
});

你偶然使用$('.more-link')吗?