Piszę rozszerzenie do Chrome i używam javascript do wyszukiwania elementów DOM. Trafiłem na stronę, która zawiera elementy <legend>
, które wyglądają mniej więcej tak:
<legend for="contact_email">Contact Email</legend>
Mój kod wykonuje następujące czynności:
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 + " );
}
Ten htmlFor jest zawsze niezdefiniowany. Działa to dla <label>
. Czy jest inny sposób uzyskania atrybutu for = dla <legend>
?
0
Larry
19 grudzień 2019, 18:52
1 odpowiedź
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') + " );
}
Uwaga- komentarz Pointy'ego jest poprawny, ponieważ <legend>
nie obsługuje atrybutu for
; obsługuje tylko Globalne atrybuty
1
Mr Lister
26 grudzień 2019, 15:56
Dziękuję Ci. To mnie załatwi.
– Larry
19 grudzień 2019, 19:20
for
nie jest rozpoznawanym atrybutem w<legend>
elementach. Możesz uzyskać wartość, ale musisz użyćgetAttribute()
.for
jest niejeden.label
. Etykieta ma atrybutfor
.legend
działać jako podpisy dla swoich elementów nadrzędnychfieldset
.fieldset
zazwyczaj zawija wiele elementów formularza i ichlabel
s.