简体   繁体   English

无需使用css悬停的onclick切换以及悬停更改类

[英]Onclick toggle as well as on hover change class without using css hover

I have 3 boxes. 我有三个盒子。 Inside each big box(red) there is a small box(black by default but grey on hover). 每个大盒子(红色)内有一个小盒子(默认为黑色,悬停时为灰色)。

  1. On hover red box the small box color should change (It's changing now so ok). 在悬停红色框上,小框颜色应更改(现在可以更改,这样就可以了)。

  2. When you click red box the black box should change to grey(which is working) and at the same time when you mouse over other boxes then it should do same as point 1(which is working too). 当您单击红色框时,黑框应变为灰色(正在工作),同时将鼠标悬停在其他框上,则它应与第1点相同(也可以工作)。 But the problem begins here. 但是问题从这里开始。 When you click the red box the black box changes to grey and when you click again that red box the grey box is not changing to black. 当您单击红色框时,黑色框变为灰色,而再次单击该红色框时,灰色框未变为黑色。 So the black box should toggle(black to grey and then grey to black) in every click to red box. 因此,在每次单击红色框时,黑框都应切换(从黑色变为灰色,然后从灰色变为黑色)。

  3. I can't use any css :hover effect. 我不能使用任何CSS:hover效果。

Jquery jQuery的

$('.libg').click(function () {
    $('this').find('.imagebg').removeClass("clicked").toggleClass('active');
    $(this).find('.imagebg').toggleClass("clicked");
}).hover(

function () {
    if (!$(this).find('.imagebg').hasClass("clicked")) $(this).find('.imagebg').addClass('active');
},

function () {
    if (!$(this).find('.imagebg').hasClass("clicked")) $(this).find('.imagebg').removeClass('active');
});

Is there anything to do with (data clicked true). 有什么关系(数据单击true)。

Here is the jfiddle -- http://jsfiddle.net/squidraj/PTZE9/5/ 这是jfiddle- http://jsfiddle.net/squidraj/PTZE9/5/

Remove extra ' ' from $('this') $('this')删除多余的''

Use 采用

$(this).find('.imagebg').removeClass("clicked").toggleClass('active');

instead 代替

$('this').find('.imagebg').removeClass("clicked").toggleClass('active');

Demo 演示版

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM