我有一个带有标签覆盖输入的表单。如下图所示
下面是相同的代码示例。
<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]">
我想使用 jquery 并在每次用户关注输入时隐藏标签。仅当输入为空或输入未处于焦点时,标签才应可见。
$("input").focus(function () {
//hide the label corresponding to this input
});
如何使用 jquery 选择与此输入对应的标签?
注意:我无法更改 html 中的任何内容,因为它是由 Drupal 中的模块生成的。
最佳答案
占位符属性是完成这项工作的正确工具:
<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
但不要忘记,无标签输入字段是不好的做法。占位符应该被视为提示。它们不能替代标签。
关于javascript - 当输入处于焦点时隐藏标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10727326/