2011-09-23 52 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'); 
    });