jQuery悬停事件在鼠标悬停时触发两次

时间:2010-01-23 21:18:50

标签: jquery hover

我正在尝试使用此jQuery临时更改悬停时div的内容:

$(document).ready( function() {
    var $ratingHtml = '';
    $('.userRating').hover(
        function() {
            $ratingHtml = $(this).html();
            alert($ratingHtml);
            $(this).html( 'Log in to rate' );
        },
        function() {
            alert($ratingHtml);
            $(this).html( $ratingHtml );            
        }
    );
});

但是,在悬停时,警报会出现两次 - 首先是原始HTML内容,然后是“登录评分”字符串。似乎发生了第二次鼠标悬停事件。我该如何解决这个问题?

3 个答案:

答案 0 :(得分:2)

我决定采用不同的解决方案 - 在叠加层中添加文字:

$('.userRating').hover(
    function() {
        $('<div class="loginMsg">Log in to rate</div>').appendTo('.userRating');
    },
    function() {
        $('.loginMsg').remove();
    }
);

CSS中有适当的样式。

答案 1 :(得分:0)

答案 2 :(得分:0)

这个问题已经被问到并回答here @ stackoverflow.com ....

你可以使用这个jQuery plugin ...它可以帮助你悬停......

相关问题