简体   繁体   English

YUI3 JavaScript:获取占位符文本的CSS

[英]YUI3 JavaScript: get CSS for placeholder text

I'm writing some JavaScript to implement placeholder text in browsers that don't have it. 我正在编写一些JavaScript,以在不带占位符的浏览器中实现占位符文本。

I've successfully styled the placeholder text in Chrome 9 and Fx 4b11 using the following style rules: 我已使用以下样式规则在Chrome 9和Fx 4b11中成功设置了占位符文本的样式:

::-webkit-input-placeholder { color: #969696 !important; }
input:-moz-placeholder { color: #969696 !important; }

Now I want my JavaScript to retrieve this data from the stylesheet in browsers that don't understand those rules so I can manually style the placeholder text. 现在,我希望我的JavaScript在不了解这些规则的浏览器中从样式表中检索此数据,以便我可以手动设置占位符文本的样式。

Does anyone know how I can do that in YUI3? 有谁知道我如何在YUI3中做到这一点? I tried: 我试过了:

YUI().use('node',function(Y) {
    var phColor = Y.all('::-webkit-input-placeholder').getStyle('color');
});

Unfortunately this just returns: "undefined: not bound to any nodes { _query="input::-webkit-input-placeholder", _nodes=}" 不幸的是,这仅返回:“未定义:未绑定到任何节点{_query =“ input ::-webkit-input-placeholder”,_nodes =}”

Anyone know how I can do this? 有人知道我该怎么做吗? Or if it's possible? 还是有可能?

If you aren't sold on using YUI exclusively (or are willing to harvest the jQuery code from a plugin and convert it to a YUI3 module) this jQuery plugin does exactly what you are trying to do: https://github.com/mathiasbynens/Placeholder-jQuery-Plugin 如果您不打算只使用YUI(或者愿意从插件中获取jQuery代码并将其转换为YUI3模块),则此jQuery插件完全可以满足您的要求: https//github.com/ mathiasbynens /占位符-jQuery的插件

I've used it in a production environment and it works great even as far back as IE6 :) 我在生产环境中使用过它,即使在IE6之前,它也能很好地工作:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM