Jquery 获取文本并在每个页面上使用aria标签
我想从每个a.parent_链接获取文本,并将其作为aria标签应用于每个相应的切换按钮(button.sub-expand_-collapse)。我想要的最终结果是,如果aria expanded=true,则添加aria label=close+(text),如果aria expanded=false,则添加aria label=expand+(text) 第一部分是正文Jquery 获取文本并在每个页面上使用aria标签,jquery,function,label,each,Jquery,Function,Label,Each,我想从每个a.parent_链接获取文本,并将其作为aria标签应用于每个相应的切换按钮(button.sub-expand_-collapse)。我想要的最终结果是,如果aria expanded=true,则添加aria label=close+(text),如果aria expanded=false,则添加aria label=expand+(text) 第一部分是正文 $(".togSubList").each(function () { var bob = $(this
$(".togSubList").each(function () {
var bob = $(this).find(".parent_link").text();
console.log(bob);
});
但我不能让每个人都在aria标签上加上一个标签
$(".togSubList").each(function () {
var bob = $(this).find(".parent_link").text();
//console.log(bob);
$(".sub-expand_collapse").each(function(){
$(this).attr("aria-label","expand " + bob)
});
});
HTML
<li class="sub_p01 togSubList">
<button class="sub-expand_collapse" aria-expanded="false">
<i class="fa fa-plus" aria-hidden="true"></i>
</button>
<a id="subLnk_01" class="parent_link menu_p01" href="m01_c01_p01.html"><strong>Space saving widgets</strong></a>
<ul aria-labelledby="subLnk_01" class="sub_items">
list of links
</ul>
</li>
链接列表
因为您已经在每个“.togSubList”中循环,所以不需要循环该列表中的每个切换按钮。只需使用“this”作为参考,并相应地针对两者
唯一的问题是,每当按钮展开/折叠时,您都需要重新运行该函数
$(".togSubList").each(function () {
var bob = $(this).find(".parent_link").text();
//console.log(bob);
$(".sub-expand_collapse[aria-expanded='false']", this).attr("aria-label","expand " + bob);
$(".sub-expand_collapse[aria-expanded='true']", this).attr("aria-label”,”close " + bob);
});
每个函数都不需要第二个元素,因为在
LI
中只有一个元素,所以只需使用节点遍历
。然后找到这样的元素。并应用属性
$(.togSubList”)。每个(函数(){
var bob=$(this.find(“.parent_link”).text();
$(this.find(“.sub-expand_-collapse[aria expanded='false']”)attr(“aria-label”,“expand”+bob);
$(this.find(“.sub-expand_-collapse[aria expanded='true']”)attr(“aria-label”,“close”+bob);
});代码>
链接列表
您的代码在我的测试中起作用:也许某处有jQuery加载错误?这很有效,谢谢。我只需要将它包装在一个in-click中,这样每次切换按钮时它都会触发。:-)很高兴帮助你:)谢谢你的帮助@partypete25。