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

发布于 2024-08-30 16:43:14 字数 192 浏览 2 评论 0原文

如果我有以下内容:

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

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

假设我无法为标签元素设置类来设置标签元素的样式,我该如何为此元素设置CSS?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

婴鹅 2024-09-06 16:43:14

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

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

您可以像任何其他元素一样设计标签:

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

我一直认为这也非常直观。那么 - 您真正想要做什么,您提出的问题实际上表明您遇到了不同的问题。

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.

流年已逝 2024-09-06 16:43:14

两个问题,两个答案:

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

    它在这里告诉 label 引用的 元素的 ID。当用户单击

    时,某些浏览器将使用它来将焦点设置在该 元素上。

  2. 如何为此元素设置 css?

    A.如果您想对所有标签元素使用 CSS:

<前><代码>标签{
/* 你的风格 */
}

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

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.

倥絔 2024-09-06 16:43:14

for 属性是标签引用的 input/textarea/select。

您仍然可以为标签分配 id

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

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

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

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>
空‖城人不在 2024-09-06 16:43:14

For 告诉标签属于哪个元素(这实际上意味着当单击标签时该元素将获得焦点)。

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

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

断爱 2024-09-06 16:43:14

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

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

它确实是一个可用性对象。

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.

唔猫 2024-09-06 16:43:14

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

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

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

"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" />
挽手叙旧 2024-09-06 16:43:14

您可以设置 id 和类 http://www.w3schools.com/tags/ tag_label.asp

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

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.

ら栖息 2024-09-06 16:43:14

使用 Html 中的 razor,我找不到分配标签 id 的最佳方法,但您可以通过以下方式分配 id:

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

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"} )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文