2011-09-23 80 views
2

默認狀態是:添加alt屬性取決於類HREF

<a href="./" class="dynamic dynamic-children menu-item"><span class="additional-background"><span class="menu-item-text">Présentation</span></span></a> 

所以如果類= RFR-打開添加ALT = 「打開」

<a alt="open" href="./" class="dynamic dynamic-children menu-item rfr-opened"><span class="additional-background"><span class="menu-item-text">Présentation</span></span></a> 

所以如果該類不有rfr-opened add alt =「關閉」

<a alt="closed" href="./" class="dynamic dynamic-children menu-item"><span class="additional-background"><span class="menu-item-text">Présentation</span></span></a> 
+1

注:一個'了'元素,不得有'alt'屬性。 – kapa

+0

我認爲你想使用'title'而不是'alt'。 –

回答

0

這應有助於:

$('.menu-item').each(function() { 
    if ($(this).hasClass('rtf-opened')) $(this).attr('alt', 'open'); 
    else $(this).attr('alt', 'closed'); 
}); 
1

屢試不爽:http://jsfiddle.net/eMagu/(我用的督察檢查ALT值)

jQuery的一個襯墊...

// set all menu items to closed, then filter just the open class and set to open 
$('.menu-item').attr('alt','closed').filter('.rfr-opened').attr('alt','open') 
0

我認爲沿線的東西。 。 。

$('a.menu-item')each(function() { 

    if($(this).hasClass('rfr-opened')) { 

     $(this).attr('alt', 'open'); 

    } else { 

     $(this).attr('alt', 'closed'); 
    } 
}); 
+0

-1:a)你正在設置'href'而不是'alt'。 b)您將根據由'var href = ..'拉取的最後一個元素將它們全部設置爲相同的值。它應該基於每個單獨元素的類別進行動態調整。 – mellamokb

+0

很好的捕獲,編輯設置正確的屬性。 –

+0

好吧現在看起來不錯。 -1刪除。 – mellamokb

0

這對我的作品

jQuery("#rfr-topnav a").click(function() { 
$('#rfr-topnav a').attr('title', 'closed'); 
$('#rfr-topnav a.rfr-opened').attr('title', 'open'); 
    });