javascript - 输入字段内的标签在聚焦时消失

标签 javascript html css forms styles

如何获取格式如下的标签:

<label for="email"> Email Address *</label>
<input id="email" type="text" name="email" value="" size="18">

在输入文本框内显示并在输入元素处于焦点或满时消失?

我找到的解决方案要求我更改 html 代码,但我不能这样做,因为它来自第三方小部件。我可以让标签显示在输入字段的顶部,但我不知道如何让它对输入字段发生的事情使用react。

最佳答案

你想要的是一个占位符。它是一个 HTML5 属性,但 IE 和旧版浏览器尚不支持。要填写,那里有几个图书馆。我用过 jquery.enablePlaceholder它工作正常。

使用 JQuery,您将能够轻松地隐藏/删除当前标签并使用其内容填充占位符属性(如果需要,插件随后将使用该属性)。

$(document).ready(function() {
    // Fetch the label and its text
    var placeholderText = $("label[for='email']").text();

    // Remove the unnecessary label
    $("label[for='email']").remove();

    // Set the placeholder attribute
    // and enable the plugin for broswer not supporting it
    $("#email").attr('placeholder', placeholderText).enablePlaceholder(); 
});

关于javascript - 输入字段内的标签在聚焦时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10839972/

相关文章:

javascript - Jquery addClass 导航不起作用

javascript - Jquery - 如何从链接 href 获取页码并用作类

javascript - Knockout 不在 mvc View 中显示数据

javascript - Ember.js 有任何附加组件来路由请求吗?

html - 我如何制作 :hover inside div style?

html - 我的 <article> 了解我的介绍背景

javascript - 在结束 div 上停止侧边栏

javascript - 如何在javascript中回到数组的开头

javascript - 自定义匹配器使 Jasmine 挂起

javascript - Google Analytics for DNN (DotNetNuke) 中的自定义标签值