最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can I use label for a label tag? - Stack Overflow

programmeradmin1浏览0评论

Usually I used label tag for pointing input tag like this

<label for='firstname'>First Name:</label>
<input type='text' id='firstname' />

Now I have this

<label for='firstname'>First Name:</label>
<label id='firstname'></label>

Since I haven't crossed anything like this before, is it possible to have label tag for a label. Will it work, when I apply Javascript because I have to update the values? or this is a valid w3c pliance?

I'm confused whether to use it or not.

Usually I used label tag for pointing input tag like this

<label for='firstname'>First Name:</label>
<input type='text' id='firstname' />

Now I have this

<label for='firstname'>First Name:</label>
<label id='firstname'></label>

Since I haven't crossed anything like this before, is it possible to have label tag for a label. Will it work, when I apply Javascript because I have to update the values? or this is a valid w3c pliance?

I'm confused whether to use it or not.

Share Improve this question edited Aug 30, 2013 at 15:26 j08691 208k32 gold badges269 silver badges280 bronze badges asked Aug 30, 2013 at 15:23 PraveenPraveen 56.5k35 gold badges136 silver badges165 bronze badges 9
  • Have you tested it with validator.w3 ? – Matthew Layton Commented Aug 30, 2013 at 15:25
  • 1 Maybe that's why you're confused. You seem to be fundamentally misunderstanding the purpose of the label element. – BoltClock Commented Aug 30, 2013 at 15:25
  • Why would you want to? – j08691 Commented Aug 30, 2013 at 15:25
  • According to this JSFiddle (jsfiddle/r9zx9) it should work. I've given the first label an ID aswell so it can be accessed by Javascripts document.getElementByID – Bjorn Smeets Commented Aug 30, 2013 at 15:25
  • The label element is monly used to focus an element, usually input... is there any other use that I don't know, or why would you want to focus a label? – André Leria Commented Aug 30, 2013 at 15:25
 |  Show 4 more ments

5 Answers 5

Reset to default 6

It's invalid according to the W3C validator.

The for attribute of the label element must refer to a form control.

No. Label elements label (primarily) form controls (<input>, <button>, etc), not arbitrary elements.

See the HTML specification:

Some elements, not all of them form-associated, are categorized as labelable elements. These are elements that can be associated with a label element.

button, input (if the type attribute is not in the hidden state), keygen, meter, output, progress, select, textarea

As others have answered, the label element must refer to a control, such as input, so it must not refer to another label element, even syntactically. This restriction is present even in HTML5.

This means that if you read user input using elements other than controls, you can still use labels for them, but you must not use label markup for the label. Normally, text data like names is best read using input type=text. If you cannot use it, or another control, for some reason, then the approach remended in ARIA specifications is to a semantically neutral element, normally span, and use the ARIA attribute aria-labelledby to specify the connection between a label and the span. Example:

<span id=firstnameLabel>First Name:</span>
<span role=textbox id=firstname aria-labelledby=firstnameLabel></span>

The ARIA attributes are primarily meant for assistive or otherwise accessibility-aware software. They are probably ignored by mainstream browsers.

Label for label is non-conforming. Probably HTML5 output element is what you want?

The Label "For" attribute has to point to a valid ID attribute of a "control" element, http://www.w3/TR/REC-html40/interact/forms.html#h-17.2 lists valid control elements as buttons, checkboxes, radio buttons, menus, file select, hidden controls, object controls.

It might work in JS, but it would be invalid HTML

发布评论

评论列表(0)

  1. 暂无评论