如何设置“标签”HTML 元素的 id?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2725585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:52:17  来源:igfitidea点击:

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

htmlcsslabel

提问by Mallika Iyer

If I have the following:

如果我有以下几点:

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

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

这里的“for”属性是什么?这是身吗?

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

假设我不能为标签元素设置一个类来设置标签元素的样式,我该如何为这个元素设置 css?

采纳答案by Alain BECKER

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 :

  1. 这里的“for”属性是什么?

    在这里告诉<input>标签引用的元素的ID 。<input>当用户单击该元素时,某些浏览器将使用它来设置对该元素的关注<LABEL>

  2. 我如何为这个元素设置 css?

    A. 如果你想 CSS 所有标签元素:

      label {
         /* your styles */
      }
      label {
         /* your styles */
      }

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

B. 如果您想标记该元素,只需像往常一样使用 ID 或类名。

回答by Tomalak

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

for属性包含标签所针对的元素的 ID。我一直认为这会很直观......

<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 reallytrying to do, the questions you ask are a sign that you have a different problem, actually.

我一直认为这也很直观。所以 - 你真正想要做什么,你提出的问题实际上表明你有一个不同的问题。

回答by ktr

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

用于属性是输入/ textarea的/选择的标签是指。

You can still assign an idto the label:

您仍然可以为标签分配一个id

<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 forattribute.

您还可以将 input/textarea/select 与标签包装起来,以便在没有for属性的情况下将它们关联起来。

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

回答by Ariel

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

For通知属于(这实际上意味着被点击标签时,元素将获得焦点),该元素的标签。

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']")

至于你的第二个问题 - 你可以使用jQuery
- 如果你的 html 是静态使用$("label:eq(index)")
- 如果你的 html 是动态的并且你知道标签所属元素的 id,你可以使用$("label[for='thatid']")

回答by Glycerine

HTML Label Tagis 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

HTML 标签标签用于表单和提交。它不是 ID,这个“for”应该与连接到它的对象的 ID 具有相同的名称 - 例如

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

Its a usability object really.

它真的是一个可用性对象。

回答by racerror

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

“for”是标签应该关联的表单元素的 id。

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

您可以在标签中添加一个 id 以直接引用它。

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

回答by Sniper

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

您可以设置一个 id 以及一个类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.

for“指定标签绑定到哪个表单元素”,因此当用户单击标签时,它会专注于目标输入。

回答by Gustavo Pérez Espinoza

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 中的 razor 我找不到分配标签 id 的最佳方法,但您可以通过以下方式分配 id:

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