jQuery 在鼠标悬停时显示图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18783225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 22:31:00  来源:igfitidea点击:

Display Image on Mouseover

javascriptjqueryhtmlcssmouseover

提问by Sarah McLachlane

How do I display an image on mouse over? I also need to underline the text next to the image on mouse over? On mouseout I have to hide the image and make the text normal again.

如何在鼠标悬停时显示图像?我还需要在鼠标悬停时在图像旁边的文本下划线吗?在鼠标移出时,我必须隐藏图像并使文本再次正常。

This is my html field

这是我的 html 字段

<img src="img.jpg"> Name

Now when I load the page I want not to display the image unless I have the mouse over the place it should appear.

现在,当我加载页面时,我不想显示图像,除非我将鼠标悬停在它应该出现的位置上。

回答by Anton

CSS

CSS

#test{
    display:none
}

HTML

HTML

<img id="test" src="img.jpg"/> <span>Name</span>

jQuery

jQuery

  $(document).ready(function () {
   $('span').on('mouseenter', function () {
       $('#test').show();
       $(this).css({
           "text-decoration": "underline"
       });
   }).on('mouseleave', function () {
       $('#test').hide();
       $(this).css({
           "text-decoration": ''
       });
   });;

});

});

DEMO

演示

Documentation

文档

jQuery

jQuery

document.ready

文档就绪

jquery.on()

jquery.on()

jQuery.show()

jQuery.show()

jQuery.hide()

jQuery.hide()

jQuery.mouseenter

jQuery.mouseenter

jQuery.mouseleave

jQuery.mouseleave

回答by Harry

You can do it the below way using the hoverevent.

您可以使用悬停事件按以下方式执行此操作。

jQuery:

jQuery:

$(document).ready(function () {
    $('span').hover(function(){
        $(this).addClass('underline'); //to make text underlined on hover
        $('#image').show(); //displays image on mouse in
    },function(){
        $(this).removeClass('underline'); //remove underline on mouse out
        $('#image').hide(); //hides image on mouse out
    });
});

HTML:

HTML:

<img class="hidden" id="image" src="img.jpg"/> <span>Name</span>

CSS:

CSS:

.hidden{
    display: none;
}
.underline{
    text-decoration: underline;
}

Working Demo

工作演示

回答by Ganesh Pandhere

As answered by Anton, you can use this one as well.

正如安东所回答的那样,您也可以使用这个。

$(document).ready(function () {    
   $('span').hover(
    function () {
        alert("1");
        $('#test').show();
    },
    function () {
        alert("2");
        $('#test').hide();
    }
   )
});

回答by Vipin Dhiman

Jquery:-

jQuery:-

    $(document).ready(function(){
       $("#textDiv").mouseover(function(){
    $("#imageDiv").hide();
   });

   $("#textDiv").mouseout(function(){
    $("#imageDiv").show();
   });
    });

HTML:-

HTML:-

    <div id="textImage">
       <div id="imageDiv" style="float:left;">
          <img src="../images/login_background.png"></img>
       </div>
       <div id="textDiv"style="float:left;">
          <a href="#">Name</a>
       </div>
</div>

CSS:-

CSS:-

    #textImage a {text-decoration:none;}
    #textImage a:hover {text-decoration:underline;}

回答by woondark

HTML

HTML

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  <title>Test</title> 
  <script type='text/javascript' src='http://identify.site88.net/jquery-1.9.1.js'></script>
  <style type='text/css'>
       #test{
        display:none
    }
  </style>
    <script type='text/javascript'>
  $(window).load(function(){
  $(document).ready(function () {
       $('span').on('mouseenter', function () {
           $('#test').show();
           $(this).css({
               "text-decoration": "underline"
           });
       }).on('mouseleave', function () {
           $('#test').hide();
           $(this).css({
               "text-decoration": ''
           });
       });;
    });
  });
  </script>
</head>
<body>
    <img id="test" src="http://www.shop.hidra.com.tr/wp-content/uploads/image_1306_1.jpg"/> <span>Name</span>
</body>
</html>