开发者

How to attach click events on selected links for a page using jQuery?

Hi I am using following code to attach event to links on the page.

$(document).ready(function(){ 
     $("a").click(function(){
     ..... do something
   });
  });

But I don't want to do this for all the links. I want to do this for few selected links. Can anyone help how can I achi开发者_开发技巧eve this?


Add a class to all of the links you want to add click events for, then:

$('.thatClassName').click(function(){ ... });


Refine your selector - instead of all links, target them by class, id, or parent etc.

$(document).ready(function(){ 
 $(".myClass1, .myClass2, #myId").click(function(){
 ..... do something

}); });


$(document).ready(function(){ 
     $(".className1").click(function(){     /*Action1*/   });
     //or
     $("#id1").click(function(){     /*Action2*/   });
  });


Add a class to the links you want to affect, then change your selector to $('a.className').

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜