本文介绍了如何在jQuery选择器中定义css:hover状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要定义一个div的背景颜色:hover with jQuery,但以下似乎不工作:

I need to define a div's background color on :hover with jQuery, but the following doesn't seem to work:

$(".myclass:hover div").css("background-color","red");

如何获得相同的结果?重要的是它必须用jQuery完成,但由于某种原因它不工作。有什么建议么?谢谢!

How can I get the same result? It's important that it has to be done with jQuery but for some reason it doesn't work. Any suggestions? Thanks!

推荐答案

我建议使用CSS超过jquery(如果可能)否则你可以使用这样

I would suggest to use CSS over jquery ( if possible) otherwise you can use something like this

$("div.myclass").hover(function() {
  $(this).css("background-color","red")
});

您可以根据需要更改选择器。

You can change your selector as per your need.

正如@ A.Wolff所说,如果你想对多个类使用悬停效果,你可以像这样使用

As commented by @A.Wolff, If you want to use this hover effect to multiple classes, you can use it like this

$(".myclass, .myclass2").hover(function(e) {
    $(this).css("background-color",e.type === "mouseenter"?"red":"transparent")
})

这篇关于如何在jQuery选择器中定义css:hover状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 04:53