简体   繁体   English

悬停和单击功能/ jQuery

[英]On hover and on click function / Jquery

I posted a question few hours back but my example was not perfect to understand. 几小时前,我发布了一个问题,但是我的例子并不完美。

So here it goes. 所以就到这里。

I have two big boxes. 我有两个大箱子。 On hover each of the big red box, the small black box should change it's bg color.When you click on each big box then the small box color should change as well.On mouse enter and out it's working fine. 将鼠标悬停在每个大红色框上时,小黑色框应更改其bg颜色。单击每个大框时,小框颜色也应更改。在鼠标进入和退出时,它都可以正常工作。 But onclick it's not working. 但是onclick无效。 I tried with bind/unbind but didn't work. 我尝试使用bind / unbind,但是没有用。

Jquery: jQuery:

$('.libg').click(function () {
    $('.imagebg').removeClass('active');
    $(this).find('.imagebg').addClass('active');
}).hover(
function () {
    $(this).find('.imagebg').addClass('active');
},
function () {
    $(this).find('.imagebg').removeClass('active');
});

Jsfiddle is http://jsfiddle.net/squidraj/kdZ8J/2/ Jsfiddle是http://jsfiddle.net/squidraj/kdZ8J/2/

Please advice. 请指教。 Thanks. 谢谢。

I would check each box separately. 我会分别选中每个框。 So I'm going to modify your html code a bit along with some jquery code. 因此,我将对您的html代码以及一些jquery代码进行一些修改。 http://jsfiddle.net/kdZ8J/15/ http://jsfiddle.net/kdZ8J/15/

Html code: HTML代码:

<ul>
  <li class="libg1" id="1">
    <div class="imagebg1" id="1a"></div>
  </li>
  <li class="libg2" id="2">
    <div class="imagebg2" id="2a"></div>
  </li>
</ul>

Handle click events: 处理点击事件:

$('#1').click(function () {
   //add the click and set active class
   $(this).data('clicked', true);
   $('#1a').addClass('active');
   //remove the click and active class
   $('#2').data('clicked', false);
   $('#2a').removeClass('active');
})
$('#2').click(function () {
   //add the click and set active class
   $(this).data('clicked', true);
   $('#2a').addClass('active');
   //remove the click and active class
   $('#1').data('clicked', false);
   $('#1a').removeClass('active');
})

First Rectugular Hovering code: 第一个矩形悬停代码:

$('.libg1').hover(function () {
  if($(this).data('clicked')) {
    //check if active class exists
    if($('.active').size()){
      $('#1a').removeClass('active');
    }
    else{
      $('#1a').addClass('active');
    }
  }
  else{
    $('#1a').addClass('active');
  }
},function () {
    if($(this).data('clicked')) {
      $('#1a').addClass('active');
    }
    else{
      $('#1a').removeClass('active');
    }
});

Second Rectangular Hoving code: 第二个矩形Hoving代码:

$('.libg2').hover(function () {
   if($(this).data('clicked')) {
     //check if active class exists
     if($('.active').size()){
       $('#2a').removeClass('active');
     }
     else{
       $('#2a').addClass('active');
     }
   }
   else{
     $('#2a').addClass('active');
   }
},function () {
   if($(this).data('clicked')) {
     $('#2a').addClass('active');
   }
   else{
     $('#2a').removeClass('active');
   }
});

You have two conflicting lines of code: 您有两行冲突的代码行:

$('.imagebg').removeClass('active');
$(this).find('.imagebg').addClass('active');

The first line will remove the class, and the second will add the class right back to the element. 第一行将删除该类,第二行将其添加回该元素。

http://jsfiddle.net/kdZ8J/4/ http://jsfiddle.net/kdZ8J/4/

$('.libg').click(function () {
    var current = $(this).find('.imagebg');
    var all = $('.imagebg');
    var index = all.index(current);

    $('.imagebg').each(function(i) {
        if(i != index) {
            $(all[i]).removeClass('active');
        }
    });

    if(current.hasClass('active')) {
        current.removeClass('active');
    } else {
        current.addClass('active');
    }
}).hover(
function () {
    $(this).find('.imagebg').addClass('active');
},
function () {
    $(this).find('.imagebg').removeClass('active');
});

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

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