简体   繁体   English

隐藏标签输入对焦时

[英]Hide Label When input is in focus

I am having a form with label overlaying the input. 我有一个标签覆盖输入的表单。 Like in the below image 如下图所示

截图

Below is a code example of the same. 下面是相同的代码示例。

<label for="edit-submitted-name--2">
Name
<span class="form-required" title="This field is required.">*</span>
</label>
<input id="edit-submitted-name--2" class="form-text required" type="text" maxlength="128" size="35" value="" name="submitted[name]">

I want to use jquery and hide the label each time a user focus on the input. 我想使用jquery并在每次用户关注输入时隐藏标签。 Label should only be visible if the input is empty or if the input is not in focus. 仅当输入为空或输入未处于焦点时,才应显示标签。

  $("input").focus(function () {
          //hide the label corresponding to this input
     });

How do i select the label corresponding to this input using jquery? 如何使用jquery选择与此输入对应的标签?

Note: I cant change anything much in the html as its generated by a module in Drupal. 注意:我不能在html中更改任何内容,因为它是由Drupal中的模块生成的。

placeholder attribute is the right tool for this job: 占位符属性是此作业的正确工具:

<input id="edit-submitted-name--2" class="form-text required" type="text" maxlength="128" size="35" placeholder="Name *" name="submitted[name]">

http://www.w3schools.com/html5/att_input_placeholder.asp http://www.w3schools.com/html5/att_input_placeholder.asp

But don't forget that label-less input fields are bad practice. 但不要忘记,无标签输入字段是不好的做法。 Placeholders should be treated like a hint. 占位符应该被视为提示。 They are not a replacement for labels. 它们不是标签的替代品。

Select the previous element from the current element. 从当前元素中选择前一个元素。

$(this).prev("label").hide();

and

$(this).prev("label").show();

However, this isn't the purpose of the label element. 但是,这不是标签元素的目的。

Remy Bach has created Super Labels which does what you want, I would use his code, but if you're looking for something slightly different then you can use his code as a template. Remy Bach创建了超级标签,它可以满足您的需求,我会使用他的代码,但如果您正在寻找稍微不同的东西,那么您可以使用他的代码作为模板。

https://github.com/remybach/jQuery.superLabels https://github.com/remybach/jQuery.superLabels

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM