Jquery 根据类向href添加alt属性

Jquery 根据类向href添加alt属性,jquery,Jquery,默认状态为: <a href="./" class="dynamic dynamic-children menu-item"><span class="additional-background"><span class="menu-item-text">Présentation</span></span></a> 因此,如果class=rfr打开,则添加alt=“打开” 所以,如果类没有打开rfr,则添加alt

默认状态为:

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

因此,如果class=rfr打开,则添加alt=“打开”


所以,如果类没有打开rfr,则添加alt=“close”


像这样的东西

$('a').attr('alt', 'closed');
$('a.rfr-opened').attr('alt', 'open');
演示:

这将有助于:

$('.menu-item').each(function () {
    if ($(this).hasClass('rtf-opened')) $(this).attr('alt', 'open');
    else $(this).attr('alt', 'closed');
});
尝试和测试:(我使用inspector检查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')

我想大概是

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

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

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

    } else {

        $(this).attr('alt', 'closed');
    }
});
这对我有用

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

注意:
a
元素不应具有
alt
属性。正如bazmegakapa所说,
元素不具有alt属性,这更适用于
。如果您需要它以某种方式引用元素,您可以将其作为类添加到标题中,我认为您想使用
title
,而不是
alt
-1:a)您正在设置
href
,而不是
alt
。b) 您将根据
var href=..
提取的最后一个元素将它们全部设置为相同的值。它应该是动态的,基于每个元素的类。
$('a.menu-item')each(function() {

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

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

    } else {

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