gpt4 book ai didi

javascript - 如何获取 HTML 元素的 "for="属性

转载 作者:行者123 更新时间:2023-12-02 22:15:10 25 4
gpt4 key购买 nike

我正在编写一个 chrome 扩展并使用 javascript 来搜索 DOM 元素。我发现一个页面有 <legend>元素看起来像这样:

<legend for="contact_email">Contact Email</legend>

我的代码正在执行以下操作:

let legends = Array.from(document.querySelectorAll('legend'));
console.log('# Legends found: ' + legends.length);
for (let l of legends ) {
console.log("Legend for= attribute: " + l.htmlFor + " );
}

这个 htmlFor 始终是未定义的。这适用于 <label> 。是否有另一种方法来获取 <legend> 的 for= 属性?

最佳答案

https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute

let legends = Array.from(document.querySelectorAll('legend'));
console.log('# Legends found: ' + legends.length);
for (let l of legends ) {
console.log("Legend for= attribute: " + l.getAttribute('for') + " );
}

注意 - Pointy 的评论是正确的 <legend>不支持属性for ;它只支持Global Attributes

关于javascript - 如何获取 HTML <legend> 元素的 "for="属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59413068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com