$(document).ready(function() { $('a, .sj_3').on('mouseover', function() { $('.kursor').addClass('hovercursor'); alert('on'); }).on('mouseout', function() { $('.kursor').removeClass('hovercursor'); }); });

Why is my jQuery mouseover/mouseout code not working? You might be encountering a common issue where your code doesn't execute because the document isn't fully loaded yet. To fix this, you need to wrap your code inside the $(document).ready() function.

This ensures that your code only runs after the entire document (including all its elements) is ready. The updated code snippet demonstrates how to implement this solution:

$(document).ready(function() {
    $('a, .sj_3').on('mouseover', function() {
        $('.kursor').addClass('hovercursor');
        alert('on');
    }).on('mouseout', function() {
        $('.kursor').removeClass('hovercursor');
    });
});

By wrapping your code within $(document).ready(), you guarantee that it executes after the document is fully loaded, preventing the issues you might have experienced. This ensures that all the elements you are targeting are available and ready to interact with, making your mouseover/mouseout events work flawlessly.


原文地址: https://www.cveoy.top/t/topic/piDd 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录