简体   繁体   中英

Toggle Class not working in hover in jquery

what i need

  • i need to toggle image when after click using toggleclass.

    css code

      .favourate{ display:block; background:url('/images/star1.png') no-repeat; width:24px; height:25px; float:right;} .favourate.faved, .favourate:active{ background:url('/images/star1_hover.png') no-repeat; transition:.2s ease all;} 

javascript code:

 $(document).ready(function(){
var eventData = {};
if (localStorage.getItem('eventData') !== null) {
    $.each(eventData = JSON.parse(localStorage.getItem('eventData')), function(id){
        $('#'+id).addClass('faved');
    });
}
$('.favourate_dextop').click(function(e){
    e.preventDefault();
    if (eventData[this.id] !== undefined) {
        delete eventData[this.id];
    }else{
        eventData[this.id] = $(this).data();
    }
    $(this).toggleClass('faved');
    localStorage.setItem('eventData', JSON.stringify(eventData));
    console.log(eventData);
});  
     });

what i have tried

debug

  • on click image changes.

  • and re click on red star noting (images do"nt changes ).

  • on removing hover it works that is not my requirement.

output

  • it should be like toggling of images without removing hover from images.

In Chrome, the Fiddles throw an error on the localStorage code, preventing the click handler from being registered. The click event code by itself works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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