2011-11-10 55 views
-2

I'm trying to bind some specific functions to specific links that have class "aMySpecialClass". Example:如何获得<a href> elements with specific class?

<a href="#" class="aMySpecialClass"></a> 

I tried several variations of the following code, no go...

jQuery('a').find('.aMySpecialClass').live(
    'click', 
    function(event) { 
     jQuery.fn.myfunction(this); 
    }); 

Can jQuery find all the using jQuery('a')?

edit: Noted that those links and classes aren't defined at run time, they are dynamically generated after the page is ready, it sends a query to the server, and with the results, it build those links with specific class afterwards.

+2

阅读jQuery文档 – bravedick

回答

3

I hate to say it, but read the docs.基本上只有CSS selector syntax

jQuery('a.aMySpecialClass').on('click', function (event) 
{ 
    jQuery.fn.myfunction(this); 
}); 
3

你试过jQuery('a.aMySpecialClass')

+0

疑难杂症!就是这样,之后我必须把它与现场结合起来。谢谢! – codenamezero

2

如果您使用find您将在内寻找元素的链接。只是做一个选择的标签和类两项:

jQuery('a.aMySpecialClass') 
3

试试这个:

$('a.aMySpecialClass').live(
    'click', 
    function(event) { 
     jQuery.fn.myfunction(this); 
}); 
+1

'.live()'在jQuery 1.7中被弃用。应该使用'.on()'。 http://api.jquery.com/live/ –

+0

这就是我最终做的,谢谢Smamatti。在我动态生成它之后,该功能才能被触发。 – codenamezero

+0

@MattBall你是对的。仍然使用旧版本,并没有想到它。 – Smamatti

相关问题