在聚焦时更改表单样式
我有一个评论表单,我将该表单的 opacity
设置为 0.7
现在我想将 opacity
更改为 1
code> 仅当用户填写该表单时。我怎样才能做到这一点?
我尝试在表单 id 上使用 :focus
选择器,但它似乎根本不起作用。
#comment_form
{
opacity: 0.7;
}
#comment_form:focus
{
opacity: 1;
}
或者也许还有其他方法可以实现这一点,这将是更好的解决方案? (比如可能使用 Javascript?)
I have a comment form and I set the opacity
for that form to 0.7
Now I want to change the opacity
to 1
only when the users are filling out that form. How can I do that?
I tried using :focus
selector on the form id but it doesn't seem to work at all.
#comment_form
{
opacity: 0.7;
}
#comment_form:focus
{
opacity: 1;
}
Or perhaps there are other ways to achieve this that would be a better solution? (Like maybe using Javascript?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当表单内的输入获得焦点时,您尝试更改该表单的不透明度。
您无法根据 CSS 中的后代元素来选择元素,因此这需要 JavaScript。
例如,(未经测试的)YUI 3 方法将是:
然后:
您将需要类似的模糊事件代码,我建议添加一个类以使用 JS 使其半透明,这样如果 JS 不透明,它始终是完全不透明的可用的。
You are trying to change the opacity of the form when an input inside that form is focused.
You can't select elements in CSS based on what descendants they have, so this requires JavaScript.
For example, an (untested) YUI 3 approach would be:
And then:
You would need similar code for the blur event, and I would recommend adding a class to make it translucent using JS so that it is always fully opaque if JS is not available.
:focus 在 IE 5-8 中效果不佳。
无论如何, :focus 伪类应该位于各个输入元素上,例如
textarea
、input
等,而不是整个表单。编辑:有关此问题的基本解决方案,请参阅此小提琴。
:focus does not work so well in IE 5-8.
Anyway, the :focus pseudo class should be on the individual input elements, like
textarea
,input
and so on, not the whole form.Edit: For a rudimentary solution to this, see this fiddle.