Show text on hover using just JS

偶尔善良 提交于 2019-12-11 13:09:30

问题


I tried a CSS solution but won't work (I believe this is a bug with the software or server I'm using). Now looking for java solution.

Put simply, I wish text to display on hover.

Here's what I've come up with but it won't work (I'm a noob)....

<DIV class="spire-menu">
    <DIV class="menu-item">Hello1</DIV>
    <DIV>Hello2</DIV></DIV>

The plan is to have Hello2 displayed perminantly. Hello1 will only display on hover.

$(".spire-menu").hover(function () {
    $(this).find('.menu-item').show();
}, function () {
    $(this).find('.menu-item').hide();
});

回答1:


CSS is better to use instead of JQuery. But if it doesn't work, here is the JQuery solution

<div class="spire-menu">
    <DIV class="menu-item" style="display:none;">Hello1</DIV>
     <DIV style="position:absolute;top:25px;">Hello2</DIV>
</div>

jQuery:

$(function(){
    $(".spire-menu").hover(function(){
      $(this).find(".menu-item").fadeIn();
    }
                    ,function(){
                        $(this).find(".menu-item").fadeOut();
                    }
                   );        
});

Here's the working Demo

Hope it will work for you.



来源:https://stackoverflow.com/questions/17626952/show-text-on-hover-using-just-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!