2012-09-05 29 views
1

我有以下標記。我想根據URL的部分匹配錨標籤。jQuery如何根據任意屬性的隨機內容選擇元素

其實我標記如下。我知道排序字段的名稱。我想根據字段名稱選擇錨點,例如。 sort:resource_locator基於此我想選擇第一個錨標記,並且我想按照方向給這個類,這樣它會爲它添加一個小的up/down箭頭。

<th><a href="/sme_leads/index/page:1/sort:resource_locator/direction:asc">Resource Locator</a></th> 
<th><a href="/sme_leads/index/page:1/sort:business_name/direction:asc">Business Name</a></th> 

我想知道如何根據sort:resource_locator信息選擇錨標籤。

我知道$ and ^分別爲結束和開始。

+0

你想添加類/箭頭當用戶點擊錨點?或只是在文件加載? –

+0

加載文檔時。 – Vins

+0

這是否需要動態,或者僅用於排序:resource_locator –

回答

2

要在其href選擇那些a元件與sort:

$('a').filter(
    function(){ 
     return this.href.indexOf('sort:') !== -1; 
    }); 

要分配基於字符串跟隨direction:串(ascdesc)一類:

$('a').filter(
    function(){ 
     return this.href.indexOf('sort:') !== -1; 
    }).addClass(
     function(){ 
      var href = this.href, 
       matches = href.match(/direction\:([a-z]+)/); 
      return matches[1]; 
     });​ 

JS Fiddle demo

相關問題