在html表单中显示占位符而不是默认值 [英] Show placeholder instead of the default value in html form

查看:374
本文介绍了在html表单中显示占位符而不是默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

A text field in html form have a default value, but I would like to show the placeholder instead of the default value. any ideas?

推荐答案

从你所说的 此处 听起来您实际上想要收听焦点模糊事件并且只是清除< input> 的内容,如果没有输入任何内容,可以使用某种缓存来恢复它。

From what you said here it sounds like you actually want to listen for the focus and blur events and just clear the contents of the <input> with some kind of cache to restore it if nothing gets typed.

<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

这篇关于在html表单中显示占位符而不是默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆