html – 输入类型文本中的背景文本

我认为这是一个容易的问题,但我不知道如何在google中搜索这个问题

我需要一个输入文本字段中的背景文本.喜欢:插入你的名字.
当我按进入字段插入我的名字,文字消失

好的,谢谢你的解决.

<input id="textfield" name="textfield" type="text" placeholder="correct" />

现在我得到第二个问题.我如何改变占位符的颜色?
这是我的文字区域:
http://jsfiddle.net/wy8cP/1/

解决方法

以下是如何使用HTML5获取占位符:
<input id="textfield" name="textfield" type="text" placeholder="enter something" />

编辑:

我不再建议将您自己的polyfills一起黑客,如下所示.您应该使用Modernizr首先检测是否需要一个polyfill,然后激活符合您需求的polyfill库.有一个很好的选择占位符polyfill listed in the Modernizr wiki.

ORIGINAL(续):

这里是一个兼容性的polyfill:

<input id="textfield" name="textfield" type="text" value="enter something" onfocus="if (this.value == 'enter something') {this.value = '';}" onblur="if (this.value == '') {this.value = 'enter something';}">

http://jsfiddle.net/q3V4E/1/

更好的垫片方法是在页面加载时运行此脚本,并将占位符放在data-placeholder属性中,因此您的标记如下所示:

<input id="textfield" name="textfield" type="text" data-placeholder="enter something">

你的js看起来像这样:

var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
    inputs[i].value = inputs[i].getAttribute('data-placeholder');
    inputs[i].addEventListener('focus',function() {
        if (this.value == this.getAttribute('data-placeholder')) {
            this.value = '';
        }
    });
    inputs[i].addEventListener('blur',function() {
        if (this.value == '') {
            this.value = this.getAttribute('data-placeholder');
        }
    });
}

http://jsfiddle.net/q3V4E/4/

相关文章

vue阻止冒泡事件 阻止点击事件的执行 &lt;div @click=&a...
尝试过使用网友说的API接口获取 找到的都是失效了 暂时就使用...
后台我拿的数据是这样的格式: [ {id:1 , parentId: 0, name:...
JAVA下载文件防重复点击,防止多次下载请求,Cookie方式快速简...
Mip是什么意思以及作用有哪些