在输入焦点上更改占位符跨度的字体颜色 [英] Change font-color of placeholder span on input focus

查看:131
本文介绍了在输入焦点上更改占位符跨度的字体颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入字段,里面有span占位符。我想要做的是在点击输入字段时更改占位符的颜色。这里有一个jsFiddle,里面有我的领域的例子。

I have an input field with a span placeholder inside of it. What I'd like to do is change the color of the placeholder when the input field is clicked. Here is a jsFiddle with an example of my field.

http: //jsfiddle.net/Vbnj2/

最好的方法是什么?

What's the best way to do that?

推荐答案

您可以使用输入元素的选择器,然后抓取前一个跨度。添加影响颜色的类。模糊删除类。

You can use a selector for the input element and then grab the previous span. Add a class that affects the color. On blur remove the class.

http:// jsfiddle .net / Vbnj2 / 1 /

http://jsfiddle.net/Vbnj2/1/

$("span.holder + input").focus( function() {
    $(this).prev('span.holder').addClass('active');
}).blur(function() {
    $(this).prev('span.holder').removeClass('active');
});

现代浏览器增加了对占位符属性。您只需添加 placeholder =我的文本作为输入属性,浏览器将根据需要自动插入/删除它。

Modern browsers have added support for the placeholder attribute. You just add placeholder="My Text" as an input attribute and the browser will automatically insert/remove it as necessary.

这篇关于在输入焦点上更改占位符跨度的字体颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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