悬停另一个时将类添加到 div (Javascript)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5034204/
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
add class to a div when hover another one (Javascript)
提问by Francis Thibault
I have a limited understanding of Javascriptan I am trying to add a class to a div when I hover another div Right now I can add a class to the same div but I would like to be able to add let's say the class blue to a div called first when I hover #second and #third.
我对 Javascriptan 的理解有限,当我悬停另一个 div 时,我试图向 div 添加一个类现在我可以向同一个 div 添加一个类,但我希望能够将蓝色类添加到一个 div当我悬停#second 和#third 时首先调用。
current code :
当前代码:
$(document).ready(function() {
$("#second, #third").hover(function(){
$(this).addClass("hover");
$(this).removeClass("hoverable");
},
function(){
$(this).removeClass("hover");
$(this).addClass("hoverable");
}
);
});
My live website can be seen at : www.designinterieurm2.com/dev
我的直播网站可以在:www.designinterieurm2.com/dev
回答by Chris Baker
$(document).ready(function() {
$('#second, #third').hover(function(){
$('#first').addClass('blue');
},
function(){
$('#first').removeClass('blue');
});
});
回答by Cristian Radu
Just replace $(this)
with $("#div_id");
只需替换$(this)
为$("#div_id");
where #div_id
is the id of the div you're trying to change.
#div_id
您要更改的 div 的 id在哪里。
回答by user7537820
$(".toptab").hover(function(){
$(this).addClass("current");
$(this).find("ul").show();
},function(){
$(this).find("ul").hide();
$(this).removeClass("current");
});