jQuery if myDiv is:visible,addclass to another element [英] jQuery if myDiv is:visible, addclass to another element
问题描述
我一直在进入jQuery,显然我错过了一些东西。
我想添加一个类到一个元素,如果另一个是可见的。
使用 display:none;
隐藏并使用 slideToggle
我有这个:
#about_me)。is(':visible')){
$(#about_me_clicker)。addClass(。about_highlight);
} else {
}
现在我假设我已经完全错误,因此这里是一个小提琴让你看。
如果另一个是:visible?
感谢。
编辑:
为了说明清楚,我将只将该类应用于一个元素。
更改:
$(#about_me_clicker)。 addClass(。about_highlight);
To:
code> $(#about_me_clicker)。addClass(about_highlight);
您不需要包括。
此外,在你的小提琴的CSS中:
.about_highlight
{
color; #f00;
}
应为:
.about_highlight
{
color:#f00;
}
另一个注意事项是,在CSS中,id优先于类。因此,即使div具有类 about_highlight
,在 #about_me_clicker
中声明的颜色将处于活动状态。
要解决这个问题,您可以使用!important
。
$ b b
.about_highlight
{
color:#f00!important;
}
要解决这个问题,只需制定一个更具体的CSS规则。
#about_me_clicker.about_highlight
{
color:#f00;
}
更新小提琴: http://jsfiddle.net/YVqJ7/20/
I've been getting into jQuery again and obviously I'm missing something.
I want to add a class to an element depending on if another one is visible.
It's hidden with display: none;
and activated with slideToggle
I have this:
if ($("#about_me").is(':visible')){
$("#about_me_clicker").addClass(".about_highlight");
} else {
}
Now I'm assuming I've gotten it totally wrong, so here's a fiddle for you to see.
How can I add this class to the certain div, if the other one is :visible?
Thanks.
Edit:
To make things clear, I will be only applying the class to the one element.
Change:
$("#about_me_clicker").addClass(".about_highlight");
To:
$("#about_me_clicker").addClass("about_highlight");
You don't need to include the .
in the class names for addClass
.
Also, in your fiddle's CSS:
.about_highlight
{
color; #f00;
}
Should be:
.about_highlight
{
color: #f00;
}
Another note, in CSS, ids take precedence over classes. So even though the div has class about_highlight
, the color declared in #about_me_clicker
will be active.
To fix this you can use !important
.
.about_highlight
{
color: #f00 !important;
}
To fix this, just make a more specific CSS rule.
#about_me_clicker.about_highlight
{
color: #f00;
}
Updated fiddle: http://jsfiddle.net/YVqJ7/20/
这篇关于jQuery if myDiv is:visible,addclass to another element的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!