【发布时间】:2017-07-10 06:39:58
【问题描述】:
我想从每个 a.parent_link 获取文本并将其作为 aria-label 应用到每个相应的切换按钮 (button.sub-expand_collapse) 上。我想要的最终结果是 if aria-expanded = true add aria-label= close + (text) and if aria-expanded = false add aria-label= expand + (text)。
第一部分给了我文字
$(".togSubList").each(function () {
var bob = $(this).find(".parent_link").text();
console.log(bob);
});
但我不能让每个人都添加一个 aria-label。
$(".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>
【问题讨论】:
-
您的代码在我的测试中有效:jsfiddle.net/8xgtcg87 也许某处存在 jQuery 加载错误?
标签: jquery function label each