在悬停时用另一个替换一个 div [英] replace one div with another on hover

查看:22
本文介绍了在悬停时用另一个替换一个 div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当鼠标悬停在一个 div 上时,我想用另一个 div 替换它.具体来说会有一个平均的话,比如苦苦挣扎"或者超出预期",我想当用户将鼠标悬停在文字平均数上时,用数值平均代替.

I want to replace one div with another when hovering over it. Specifically there will be an average in words, such as "struggling" or "exceeding expectations", and I want to replace this with the numerical average when the user hovers over the average in words.

#html

<div class="switch avg_words float_left">
  A+ (hover to see score)  
</div>
<div class="avg_num">
  AVG = 98.35%
</div>

#css

.avg_num {
display: none;
}

#jquery

$('.switch').hover(function() {
    $(this).closest('.avg_words').hide();
    $(this).next('div').closest('.avg_num').show();
}, function() {
    $(this).next('div').closest('.avg_num').hide();
    $(this).closest('.avg_words').show();
});

隐藏第一个 div 并用第二个 div 替换它很容易,但问题在于当悬停结束时将事情恢复正常的代码.现在悬停时,div 只是在彼此之间来回闪烁.

It's easy enough to hide the first div and replace it with the second, but the trouble is with the code to set things back to normal when hover ends. Now on hover, the divs just blink back and forth between each other.

http://jsfiddle.net/uXeg2/1/

推荐答案

将 switch 类移动到外部 div,像这样

Move the switch class to an outter div, like so

<div class="switch">
<div class="avg_words float_left">
  A+ (hover to see score)  
</div>
<div class="avg_num">
  AVG = 98.35%
</div>
</div>

$('.switch').hover(function() {
        $(this).find('.avg_words').hide();
        $(this).find('.avg_num').show();
    }, function() {
        $(this).find('.avg_num').hide();
        $(this).find('.avg_words').show();
});

更新的小提琴:http://jsfiddle.net/uXeg2/2/

这篇关于在悬停时用另一个替换一个 div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆