dongzang5815 2012-07-13 20:40
浏览 351
已采纳

如何在悬停时更改文本并在鼠标移出时恢复为默认值?

I have a div which shows count like this:

How can I change the text from 0 to any word for example, when mouse is over the div, and revert to 0 when mouse leaves.

I tried this code, but it didn't work.

$('.post-fav a').bind('mouseenter',function() {
    var default_text = $(this).text();
    $(this).css({'background-position' : 'left bottom', 'color' : '#1871a4'});
    $(this).html('<?php _e('favorite','deluxe'); ?>');
});
$('.post-fav a').bind('mouseleave',function() {
    $(this).css({'background-position' : 'left top', 'color' : '#666'});
    $(this).text(default_text);
});

The default text is variable, each element has specific count, I need someway to store the initial count when mouse moves over ..I can't hard code it in js as 0 or any other count.

  • 写回答

3条回答 默认 最新

  • douzhiji2020 2012-07-13 20:44
    关注

    You just need to use the hover function, the first parameter is mouseover, the second is mouseout:

    $('div').hover(
        function() { $(this).html('anyword'); },
        function() { $(this).html('0'); }
    );
    

    EDIT:

    var storedtext;
    $('div').hover(
        function() { 
            storedtext = $(this).html();
            $(this).html('anyword'); 
        },
        function() { $(this).html(storedtext); }
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法