简体   繁体   English

隐藏图像并在悬停Jquery上显示文本

[英]Hide image and show text on hover Jquery

I have the following code: 我有以下代码:

 $(".service-link").mouseover(function () { $(this).find('img:first').hide(); var txt = $(this).attr("data-text"); $(this).text(txt); }).mouseout(function () { $(this).text(); $(this).find('img:first').show(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul id="servicesNav"> <li class="servSep"><a class="service-link" data-text="Name A" href="#"><img id="serv-image-1" src="content/images/serv-1.png" /></a></li> <li class="servSep"><a class="service-link" data-text="Name A" href="#"><img id="serv-image-8" src="content/images/serv-8.png" /></a></li> <li class="servSep"><a class="service-link" data-text="Name C" href="#"><img id="serv-image-3" src="content/images/serv-3.png" /></a></li> </ul> 

Now what I'm trying to do it on hovering on each or the li , I am trying to hide the image and show a text instead of the image and on hover out it goes back to show the image without the text. 现在,我想在将鼠标悬停在每个或li ,我试图隐藏图像并显示文本而不是图像,并且在悬停时它会返回以显示没有文本的图像。 This is where I get stuck. 这就是我卡住的地方。 The image changes to the text but it stays on the text when I'm not hovering anymore, so the image doesn't come back. 图像更改为文本,但是当我不再悬停时,它将停留在文本上,因此图像不会再出现。

Can anyone help on this? 有人可以帮忙吗?

 $(".service-link").mouseover(function() { $(this).find('img').toggle(); $(this).find('span').toggle(); }).mouseout(function() { $(this).find('img').toggle(); $(this).find('span').toggle(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul id="servicesNav"> <li class="servSep"> <a class="service-link" data-text="Name A" href="#"> <img id="serv-image-1" src="content/images/serv-1.png" /> <span style="display:none">Name A</span> </a> </li> <li class="servSep"> <a class="service-link" data-text="Name A" href="#"> <img id="serv-image-8" src="content/images/serv-8.png" /> <span style="display:none">Name B</span> </a> </li> <li class="servSep"> <a class="service-link" data-text="Name C" href="#"> <img id="serv-image-3" src="content/images/serv-3.png" /> <span style="display:none">Name C</span> </a> </li> </ul> 

  1. Create a span with the text. 用文本创建一个跨度。
  2. Toggle both img and span accordingly 切换img和跨度

You can simply use css to do that. 您可以简单地使用css来做到这一点。 No need js. 不需要js。

 ul li a:hover img { display: none } ul li a:hover span { display: block } ul li a img { display: block } ul li a span { display: none } 
 <ul id="servicesNav"> <li class="servSep"><a class="service-link" data-text="Name A" href="#"><span>Name A</span><img id="serv-image-1" src="content/images/serv-1.png" /></a></li> <li class="servSep"><a class="service-link" data-text="Name B" href="#"><span>Name B</span><img id="serv-image-8" src="content/images/serv-8.png" /></a></li> <li class="servSep"><a class="service-link" data-text="Name C" href="#"><span>Name C</span><img id="serv-image-3" src="content/images/serv-3.png" /></a></li> </ul> 

It can be achieved by just using css. 只需使用CSS即可实现。 Toggle display property on hover of parent. 切换父项悬停时的显示属性。

Example Snippet: 示例片段:

 #servicesNav .servSep span { display: none; } #servicesNav .servSep:hover span { display: inline; } #servicesNav .servSep:hover .image-tag { display: none; } 
 <ul id="servicesNav"> <li class="servSep"> <a class="service-link" data-text="Name A" href="#"> <img id="serv-image-1" class="image-tag" src="content/images/serv-1.png" /> <span>SomeText</span> </a> </li> <li class="servSep"> <a class="service-link" data-text="Name A" href="#"> <img id="serv-image-8" class="image-tag" src="content/images/serv-8.png" /> <span>SomeText</span> </a> </li> <li class="servSep"> <a class="service-link" data-text="Name C" href="#"> <img id="serv-image-3" class="image-tag" src="content/images/serv-3.png" /> <span>SomeText</span> </a> </li> </ul> 

Note: Use jQuery or css whichever you find more comfortable. 注意:请使用jQuerycss以其感到更舒适。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM