How to use if- else to check the mousehover?

后端 未结 1 1531
-上瘾入骨i
-上瘾入骨i 2021-01-28 05:36

Currently I trying to check the mousehover on the Image, if mouse is pointing on the image then it will take another image to replace the original image

here is my code:

相关标签:
1条回答
  • 2021-01-28 06:09

    You did not handle the hover event well. Do this;

    var sourceSwap = function () {
            var $this = $(this);
            var newSource = $this.data('alt-src');
            $this.data('alt-src', $this.attr('src'));
            $this.attr('src', newSource);
        }
    
    $('img[data-alt-src]').hover(sourceSwap);
    

    Also, place it inside a document ready event to make sure all the dom is fully loaded before attaching the event like this;

    $(document).ready(function(){
    .....Your codes here
    
    
    });
    
    0 讨论(0)
提交回复
热议问题