基于子对象的属性将样式应用于父级,使用jQuery? [英] Apply style to parent based on property on child, with jQuery?

查看:81
本文介绍了基于子对象的属性将样式应用于父级,使用jQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果元素具有特定属性,如何将样式应用于元素的父元素?这里是我使用的例子...

How do I apply a style to the parent of an element if it has a specific property? Here's the example I'm working with...

<div>
    <div class="inner"></div>
    <div class="inner"></div>
    <div class="inner"></div>
</div>
<div>
    <div class="inner active"></div>
    <div class="inner"></div>
    <div class="inner"></div>
</div>
<div>
    <div class="inner"></div>
    <div class="inner"></div>
    <div class="inner"></div>
</div>

...我想对out div应用一个背景颜色, inner'类也包含'active'类。这里的jQuery解决方案我黑客一起,似乎不工作...

...I want to apply a background color to the out div when its child element with an 'inner' class also contains the 'active' class. Here's the jQuery solution I hacked together, which doesn't seem to be working...

if ($("div.inner").hasClass("active")) {
    $(this).parent().css("background", "yellow");
};

有没有人有一个修复或更好的方法?非常感谢。

Does anyone have a fix, or better approach, in mind? Thanks.

此外,如何让它应用基于'inner'div中的特定文本的样式,而不是应用于特定类?再次感谢。

Also, optionally, how do I get it to apply the style based on specific text within the 'inner' div as opposed to a specific class being applied to it? Thanks again.

推荐答案

$("div.inner.active").parent().css("background", "yellow");

jsFiddle: http://jsfiddle.net/g4gBx/1/

jsFiddle: http://jsfiddle.net/g4gBx/1/

这篇关于基于子对象的属性将样式应用于父级,使用jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆