Using hasClass() with jQuery delegate?
I have a gridview (gdvMyInfo) that contains q column of links I have grabbing with jQuery.
$('#<%= gdvMyInfo.ClientID %>').delegate('a', 'click', function(){
var link = $(this).attr('href');
....
retu开发者_如何学编程rn false;
});
This was working great, but now there will be a second column containing links and I need to turn on paging. This is now attaching my jQuery changes to these new links.
I have a class of "myInfoClass" on the links I need but using the class as the selector does not seem to work for me (not sure if it is the delegate as without that it selects fine).
$('.myInfoClass').delegate('a', 'click', function(){
this does not work, so I was thinking I could use the .hasClass() function but that is failing also.
$('#<%= gdvMyInfo.ClientID %>').hasClass('myInfoClass').delegate('a', 'click', function(){
This fails with a error message of ".delegate is not a function"
So, any idea on how I can chain a selector using delegate to grab any <a>
in my gridvie "gdvMyIfno" with a class of "myInfoClass" when using a delegate?
Just add the class to the selector:
$('#<%= gdvMyInfo.ClientID %>').delegate('a.myInfoClass', 'click', function(){
Now the click
will only fire for <a>
elements that have the class myInfoClass
.
The way .delegate()
works is that it places a handler on the container #<%= gdvMyInfo.ClientID %>
. Then elements clicked inside that container get compared to the a.myInfoClass
selector. If the element matches the selector, the handler is invoked.
精彩评论