How to set the id of a 'label' HTML element?

梦想的初衷 提交于 2019-12-10 14:58:09

问题


If I have the following:

             <label for="deletetxt">Delete This Text</label>

What is the 'for' attribute here? Is that the id?

Assuming that I cannot set a class for the label element to style the label element, how do i set css for this element?


回答1:


Two question, two answers:

  1. What is the 'for' attribute here?

    It's here to tell the ID of the <input> element that label refers to. Some browsers will use it to set focus on that <input> element when user clicks on that <LABEL>

  2. how do i set css for this element?

    A. If you want to CSS all label elements :

      label {
         /* your styles */
      }

B. If you want to label that element, just use IDs or classnames as you usually do.




回答2:


The for attribute contains the ID of the element that the label is for. I always thought this would be quite intuitive...

<label for="SomeTextField" id="SomeLabel">Some text field</label>
<input type="text" id="SomeTextField">

You style a label like any other element:

label {
  font-weight: bold;
  color: red;
}

I always thought this would be quite intuitive, as well. So - what are you really trying to do, the questions you ask are a sign that you have a different problem, actually.




回答3:


The for attribute is the input/textarea/select that the label refers to.

You can still assign an id to the label:

<label id="myLabel" for="deletetxt">Delete This Text</label>

You can also wrap the input/textarea/select with the label in order to associate them without the for attribute.

<label id="myLabel">Delete This Text <input ... /></label>



回答4:


The For tells the label which element to belong to (which really means that when the label is clicked the element will get the focus).

As for your second question - you can use jQuery:
- If your html is static use $("label:eq(index)")
- If your html is dynamic and you know the id of the element the label belongs to, you can use $("label[for='thatid']")




回答5:


HTML Label Tag is used for forms and submittion. it is not the ID, this 'for' should have the same name as the ID of the object connected to it - for example

<form>
<label for='ford'>Ford Car</label>
<input type="radio" name="fordCar" id="ford" />
</form>

Its a usability object really.




回答6:


"for" is the id of the form element that the label should be associated with.

You can add an id to the label to reference it directly.

<label for="fname" id="lbl-fname">First:</label>
<input type="text" id="fname" />



回答7:


you can set an id as well as a class http://www.w3schools.com/tags/tag_label.asp

the for "Specifies which form element a label is bound to" so when a user clicks on the label it focuses on the target input.




回答8:


With razor in Html I don´t find the best way for assign the id of a label, but you can assign the id in this way:

@Html.Label("© Integrantes Grupo:",new { @id="TitleIntegrants"} )


来源:https://stackoverflow.com/questions/2725585/how-to-set-the-id-of-a-label-html-element

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!