如何在javascript中获取某个标记的元素值?

如何在javascript中获取某个标记的元素值?,javascript,Javascript,例如,我有这个代码 <label class="btn btn-default active" id="monday" aria-pressed="true"> <input type="checkbox" autocomplete="off"> Monday </label> 使用.getAttribute(): 小提琴: MDN:aria pressed是

例如,我有这个代码

<label class="btn btn-default active" id="monday" aria-pressed="true">
<input type="checkbox" autocomplete="off"> Monday
</label>
使用
.getAttribute()

小提琴:


MDN:

aria pressed是label元素上的属性,因此需要使用getAttribute方法

document.getElementById(“星期一”).getAttribute('aria-pressed')

使用
getAttribute()
方法

document.getElementById("monday").getAttribute('aria-pressed');

.getAttribute('attributeName')
;重复我的答案。所有答案都是一样的。他们也都是在同一分钟发布的。你的观点是什么。然而,它们不是在同一秒发布的。它们只差不到20秒。如果有什么问题的话,你的答案是杰拉尔多评论的副本,比我落后8秒14秒,但谁在数呢?;^)当我发布我的答案时,我没有看到Gerardo的评论。我的答案的副本。谢谢。我现在明白了。这是一个属性。@user3080440如果解决了您的问题,您应该接受其中一个答案。很乐意帮忙!挑选出来的。起初我没有注意到检查图标。我只是投了更高的票。如果我这样做,为什么会得到空结果。var dayIfActive=document.getElementById(“星期一”).getAttribute(“aria-pressed”);console.log(dayIfActive);
document.querySelector("#monday").getAttribute('aria-pressed');
document.getElementById("monday").getAttribute('aria-pressed');