当孩子获得焦点时,jQuery CSS 设置父 div 的样式
我有一个像这样的脚本:(但它不起作用)
$('div#selectbox select:focus').parent().css('box-shadow','0 0 4px 1px #5099E2');
当用户专注于父级为 div#selectbox
的 select
时,我想更改 box-
有什么建议吗?div
的shadow
这是我正在做的 jsFiddle... http://jsfiddle.net/2BydK/
I have a script like this: (but it's not working)
$('div#selectbox select:focus').parent().css('box-shadow','0 0 4px 1px #5099E2');
When the user focuses on a select
that's parent is div#selectbox
I want to change the box-shadow
of the div
any suggestions?
Here is a jsFiddle of what I'm doing... http://jsfiddle.net/2BydK/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
看起来代码可以很好地找到聚焦的选择元素,但没有任何东西可以触发它。也许可以试试这个:
如果您想在其中添加检查,您可以随时放入
if($(this).is(":focus")){}
以确保万无一失。希望有帮助...It seems like the code will do a good job of finding a focussed select element, but there is nothing to trigger it. Perhaps try this:
If you wanted to add an check in there, you could always put in a
if($(this).is(":focus")){}
just to be sure. Hope that helps...尝试使用 jQuery
focus()
try the jQuery
focus()
instead试试这个:
try this:
因为您希望在焦点期间应用样式,所以您想抓住
focus
选择框的事件,然后将样式应用到其父级。since you want to style to be applied to during the focus you want to grab the
focus
event of the select box, then apply the style to it's parent.您现在可以在纯 CSS 中执行此操作,因此不需要 JavaScript。
新的 CSS 伪类
:focus-within
将有助于解决此类情况,并且当人们使用选项卡进行导航时(在使用屏幕阅读器时很常见),将有助于提高可访问性。您可以检查哪些浏览器支持此http://caniuse.com/#search=focus-within
You can now do this in pure CSS so no JavaScript is needed.
The new CSS pseudo-class
:focus-within
would help for cases like this and will help with accessibility when people use tabbing for navigating, common when using screen readers.You can check which browsers support this http://caniuse.com/#search=focus-within