html 格式的文本字段有一个默认值, 但我想显示占位符而不是默认值。 有什么想法吗?
最佳答案
根据你所说here听起来你真的想监听 focus 和 blur 事件,只是清除 <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');
});
关于javascript - 在 html 表单中显示占位符而不是默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17800328/