jQuery - 当元素可见时删除背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20055543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:56:11  来源:igfitidea点击:

jQuery - remove background-color when an element is visible

jquerybackground-colorvisible

提问by didi

I would like to remove the background-color (only the background-color) of the menu once another element is visible. I wrote this code, but it doesn't work - anybody can help?

一旦另一个元素可见,我想删除菜单的背景颜色(仅背景颜色)。我写了这段代码,但它不起作用 - 有人可以帮忙吗?

$(function() {
if($("#wrapperHome").is(":visible")) {
    $("#menu a").css({ "background-color", "black" });  
}
});

The menu has this background style sheet information.

菜单具有此背景样式表信息。

background:url(img/official/menu.png) center center no-repeat #f2f2f7;

回答by Charlie74

I believe you want something like this...

我相信你想要这样的东西......

$("#menu a").css("background-color", ""); 

Setting the background-color to "" essentially removes the styling, removing the color.

将 background-color 设置为 "" 实质上是删除样式,删除颜色。

回答by tymeJV

Use :not ,when doing key/val CSS changes:

在进行 key/val CSS 更改时使用:not ,

$("#menu a").css({ "background-color" : "black" }); 

Or since it's one value:

或者因为它是一个值:

$("#menu a").css("background-color", "black"); 

回答by Jared

You should move the style into the css for your site. This avoids having css littered through out your js and you can then re-use it.

您应该将样式移动到您网站的 css 中。这避免了 css 散落在你的 js 中,然后你可以重新使用它。

.selected-menu-item{ // or what ever it is that has a black background
  background-color: black;
}

Then in your jquery:

然后在你的 jquery 中:

$("#menu a").toggleClass("selected-menu-item");