当文本区域处于焦点时,CSS 如何设置父项的样式?
我有以下内容
<div class="comment-wrap">
<textarea></textarea>
</div>
每当文本区域处于焦点时,我想设置评论换行 div 的样式。我试过了:
#hallPost-inner + textarea:focus {
background: red;
}
这不起作用。关于如何在文本区域处于焦点时设置评论换行 div 的样式(意味着光标在文本区域中闪烁并且用户可以键入)有什么想法吗?
谢谢
I have the following
<div class="comment-wrap">
<textarea></textarea>
</div>
Whenever the textarea is in focus, I want to style the comment-wrap div. I've tried:
#hallPost-inner + textarea:focus {
background: red;
}
This does not work. Any ideas on how I can style the comment-wrap div whenever the textarea is in focus, meaning the cursor is blinking in the textarea and the user can type?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不确定
#hallPost-inner
是什么,但一般来说 CSS 选择器不能上升,这意味着您需要有textarea:focus
选择器,但随后需要设置样式祖先元素,这是 css 中无法完成的。 这里是一个很好的资源,还有许多其他资源。该链接还展示了如何实现简单的 JavaScript 解决方案。Not sure what
#hallPost-inner
is, but in general CSS selectors cannot ascend, meaning you would need to have thetextarea:focus
selector, but then would need to style an ancestor element, which cannot be done in css. Here's a good resource, among many others. The link shows how an easy javascript solution can be achieved as well.使用伪选择器
:focus-within
。请注意,任何获得焦点的子项都会影响父项。如果你有更多的孩子并且只想在事件触发更多详细信息的链接。 (css-tricks)
例如:
注意:并非所有浏览器都支持
:focus-within
Use the pseudo selector
:focus-within
. Note that any child in focus will effect the parent. If you have more children and just wanna apply when the event hits the<textarea>
, you will need JS.Link for more details. (css-tricks)
Ex.:
Note: not all browsers support
:focus-within
使用 JavaScript 和 jQuery,您可以执行以下操作:
With JavaScript and jQuery, you could do: