2011-11-10 59 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

相關問題