当选择文本输入时如何更改 CSS?
我想在选择文本框时更改 的 CSS;即当用户单击文本框时,我希望应用不同的 CSS 规则。有没有办法使用纯 CSS 来做到这一点?
I want to change the CSS of an <input type="text">
when the text box is selected; i.e. when the user clicks on the text box, I want different CSS rules to apply. Is there any way to do this using pure CSS?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您是否尝试过焦点伪类
更多信息: http://reference.sitepoint.com/css/伪类焦点
Have you tried the focus pseudo-class
more info: http://reference.sitepoint.com/css/pseudoclass-focus
选择仅具有文本属性的输入字段
选择任何输入字段(复选框、文件、隐藏、图像、密码、单选、重置、提交、文本)
选择任何文本区域
好的,这是由 CSS 实现的,但是如果您希望实现平滑的动画效果并且可以更好地控制焦点/模糊,最好使用 JQuery 或其他 JavaScript 框架。
select the input fields which have attribute of text only
Select any input field (checkbox,file,hidden,image,password,radio,reset,submit,text)
Select any textarea
OK that's by CSS however if you are looking to achieve smooth animated effects and have more control over focus/blur it is better to use JQuery or other JavaScript frameworks.
是的。使用
:focus
伪类选择它。演示位于 http://dabblet.com/gist/1490497
Yes. Select it using the
:focus
pseudoclass.Demo at http://dabblet.com/gist/1490497
中使用以下内容
您可以在 CSS E::selection
,其中 E 是元素。
http://www.456bereastreet.com/archive/200601/css_3_selectors_explained/
您可以还可以使用 jquery 使用
You can use the following in CSS
E::selection
where E is the element.
http://www.456bereastreet.com/archive/200601/css_3_selectors_explained/
You can also use jquery using the
尝试这样。对于失去焦点,您可以尝试 onblur 而不是 onclick。
Try like this. for lost focus you may try onblur instead of onclick.