Javascript 背景色不变?
因此,它会更改背景图像,但不会更改背景颜色。 不管怎样,你愿意指出我代码中的问题吗 JavaScript:Javascript 背景色不变?,javascript,jquery,css,Javascript,Jquery,Css,因此,它会更改背景图像,但不会更改背景颜色。 不管怎样,你愿意指出我代码中的问题吗 JavaScript: $("#menu a").hover(function() { $(this).addClass("hover"); }, function() { $(this).removeClass("hover"); }); CSS: 黑暗中的一枪: 在将事件绑定到DOM之前,需要确保这些节点存在于DOM中。如果您在中调用您的,请等待文档。准备好了吗: jQuery(functi
$("#menu a").hover(function() {
$(this).addClass("hover");
}, function() {
$(this).removeClass("hover");
});
CSS:
黑暗中的一枪:
在将事件绑定到DOM之前,需要确保这些节点存在于DOM中。如果您在
中调用您的
,请等待文档。准备好了吗:
jQuery(function($){
$("#menu a").hover(function() {
$(this).addClass("hover");
}, function() {
$(this).removeClass("hover");
});
});
或者,您可以通过使用带有“其他位置”选项的选择器来覆盖背景色
属性。如果定义了诸如a:link
、a:visted
、a:hover
、a:focus
或a:active
等样式,则需要更高的专用性选择器来覆盖它:
a.hover {
/* your styles here */
}
您需要为我们提供更多的CSS,以便为您提供更好的建议
您可能拼错了一个ID,或者没有将a
元素嵌套在另一个元素下,使用上面提到的[ID=“menu”]
。。。如果您只添加以下css,它将处理它
a:hover {
background-color: white;
background-position: top center;
background-image: url("img/dot.png");
background-repeat: no-repeat;
color: red;
}
:hover是一个伪css类,您可以在任何东西上使用它,而无需添加jquery/js来支持它 根据的评论,您的css中可能有另一条规则覆盖背景色,这反过来可能是一个问题
您可以通过稍微更改悬停
功能来测试这一点:
$("#menu a").hover(
function () {
//add 'background-color' as an inline css style which will have higher specificity than anything in the css.
$(this).addClass("hover").css('background-color', 'white');
}, function () {
$(this).removeClass("hover").css('background-color', '');
}
);
你的问题是有东西覆盖了你的背景色。使用firebug或其他DOM检查器查找您的问题。修补程序可以使背景色变得重要,但您只能在测试时使用此选项:
background-color: black !important;
如果这样做有效,您仍然需要找出是什么覆盖了您的背景
然后,您可以使用纯CSS来实现这一点
#menu a:hover {
background-color: black;
background-position: top center;
background-image: url("img/dot_hover.png");
background-repeat: no-repeat;
color: red;
}
您需要显示非悬停默认类。可能是其他css规则正在页面上覆盖它。您不必使用$。hover()
;您可以使用:hover
伪类在纯CSS中执行此操作。您很可能遇到CSS定义相互覆盖的问题-假设jQuery是正确的…修补程序-背景色:白色!重要的代码>
#menu a:hover {
background-color: black;
background-position: top center;
background-image: url("img/dot_hover.png");
background-repeat: no-repeat;
color: red;
}