javascript - 在 html 表单中显示占位符而不是默认值

标签 javascript html twitter-bootstrap forms

html 格式的文本字段有一个默认值, 但我想显示占位符而不是默认值。 有什么想法吗?

最佳答案

根据你所说here听起来你真的想监听 focusblur 事件,只是清除 <input> 的内容如果没有输入任何内容,使用某种缓存来恢复它。

<input id="foo" type="text" value="" data-value="" />

然后在脚本中

var foo = document.getElementById('foo');
foo.addEventListener('focus', function () {
    this.setAttribute('data-value', this.value);
    this.value = '';
});
foo.addEventListener('blur', function () {
    if (this.value === '')
        this.value = this.getAttribute('data-value');
});

DEMO

关于javascript - 在 html 表单中显示占位符而不是默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17800328/

相关文章:

python - 使用 python/beautifulsoup 抓取没有 id 的表,如何使用文本 html 字符串?

jquery - 修改 Bootstrap 导航栏

html - 我无法使 Bootstrap 表单响应

javascript - 如何查询 mongoose 返回值

在对象上找不到 JavaScript 原型(prototype)方法

javascript - AJAX Date 无法正确显示日期

javascript - 当在 iframe 中指定全局变量名称时,表单目标中断

html - 使用 Hypermedia Constraint API 驱动 UI

javascript - 关闭表折叠行

javascript - ExpressJS 错误处理不起作用