当孩子获得焦点时,jQuery CSS 设置父 div 的样式

发布于 2024-12-22 22:37:11 字数 370 浏览 3 评论 0原文

我有一个像这样的脚本:(但它不起作用)

$('div#selectbox select:focus').parent().css('box-shadow','0 0 4px 1px #5099E2');

当用户专注于父级为 div#selectboxselect 时,我想更改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

幸福还没到 2024-12-29 22:37:11

看起来代码可以很好地找到聚焦的选择元素,但没有任何东西可以触发它。也许可以试试这个:

$('div#selectbox select').focus(function(){
    $(this).parent().css('box-shadow','0 0 4px 1px #5099E2');
});

$('div#selectbox select').blur(function(){
    $(this).parent().css('box-shadow','none');
});

如果您想在其中添加检查,您可以随时放入 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:

$('div#selectbox select').focus(function(){
    $(this).parent().css('box-shadow','0 0 4px 1px #5099E2');
});

$('div#selectbox select').blur(function(){
    $(this).parent().css('box-shadow','none');
});

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...

剧终人散尽 2024-12-29 22:37:11

尝试使用 jQuery focus()

$('div#selectbox select').focus(function() {
  $(this).parent().css('box-shadow','0 0 4px 1px #5099E2');
});
div { margin: 10px }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="selectbox">
  <select>
    <option>option one</option>
    <option>option two</option>
  </select>
</div>

try the jQuery focus() instead

$('div#selectbox select').focus(function() {
  $(this).parent().css('box-shadow','0 0 4px 1px #5099E2');
});
div { margin: 10px }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="selectbox">
  <select>
    <option>option one</option>
    <option>option two</option>
  </select>
</div>

剪不断理还乱 2024-12-29 22:37:11

试试这个:

$('div#selectbox select').focus(function () {
    $(this).parent().css('box-shadow','0 0 4px 1px #5099E2');
});

try this:

$('div#selectbox select').focus(function () {
    $(this).parent().css('box-shadow','0 0 4px 1px #5099E2');
});
飞烟轻若梦 2024-12-29 22:37:11

因为您希望在焦点期间应用样式,所以您想抓住 focus 选择框的事件,然后将样式应用到其父级。

$('div#selectbox select').focus( function() {
  $(this).parent().css('box-shadow','0 0 4px 1px #5099E2')
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="selectbox" style="margin: 20px; padding:10px;">
  <select>
    <option>1</option>
    <option>2</option>
  </select>
</div>

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.

$('div#selectbox select').focus( function() {
  $(this).parent().css('box-shadow','0 0 4px 1px #5099E2')
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="selectbox" style="margin: 20px; padding:10px;">
  <select>
    <option>1</option>
    <option>2</option>
  </select>
</div>

最丧也最甜 2024-12-29 22:37:11

您现在可以在纯 CSS 中执行此操作,因此不需要 JavaScript。

新的 CSS 伪类 :focus-within 将有助于解决此类情况,并且当人们使用选项卡进行导航时(在使用屏幕阅读器时很常见),将有助于提高可访问性。

div#selectbox:focus-within {
    box-shadow: 0 0 4px 1px #5099E2;
}

:focus-within 伪类匹配元素本身
匹配 :focus 或具有匹配 :focus 的后代。

您可以检查哪些浏览器支持此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.

div#selectbox:focus-within {
    box-shadow: 0 0 4px 1px #5099E2;
}

The :focus-within pseudo-class matches elements that either themselves
match :focus or that have descendants which match :focus.

You can check which browsers support this http://caniuse.com/#search=focus-within

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文