繁体   English   中英

如何获取 HTML 的“for=”属性<legend>元素

[英]How to get the "for=" property of an HTML <legend> element

我正在编写一个 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') + " );
}

注意-尖尖的评论是在正确的<legend>不支持该属性for ; 它只支持全局属性

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM