2011-07-07 42 views
0

我的頁面有兩個「標籤」導航部分。兩者都相互獨立,但導致相同的結果。我想最簡單的方法是將它們編碼在一起工作,找到兩個部分中相同的'href'屬性,將其保存在一個變量中,然後從那裏繼續。jQuery找到類似的屬性

我的佈局看起來像這樣。

<div id="tab-text"> 
    <a href="tab-1"></a> 
    <a href="tab-2"></a> 
    <a href="tab-3"></a> 
</div> 

<div id="tab-arrow"> 
    <a href="tab-1"></a> 
    <a href="tab-2"></a> 
    <a href="tab-3"></a> 
</div> 

而我有一些這樣的jQuery。

jQuery('#tab-text a').click(function() 
{ 
    jQuery('#tab-text a').removeClass('active'); 
    jQuery(this).addClass('active'); 
} 

所以,我怎麼能包括jQuery的,如果#tab-text a被點擊,比找到href屬性的值,而不是從#tab-arrow a搜索該值和.addClass()

回答

0
jQuery('#tab-text a').click(function() 
{ 
    jQuery('#tab-text a').removeClass('active'); 
    jQuery(this).addClass('active'); 
    var searchHref = this.href; 
    jQuery('#tab-arrow a[href="'+searchHref+'"]').doSomething(); 
} 
+0

請問'this.href 「工作?我相信它應該是'$(this).attr('href')'。 – unclenorton

+0

@unclunorton - 不需要額外的jQuery對象。 'this.href'也可以工作(也許aven更好) – Neal

+0

嗯我寫了基本相同的東西......'var active_tab = jQuery(this).attr('href');'很奇怪爲什麼它不工作.. 。 – cnotethegr8