简体   繁体   English

在Img Hover上,在另一个div中显示文本

[英]On Img Hover show text in another div

I'm trying to find a way to do this: 我正在尝试找到一种方法来做到这一点:

I've a images with a text inside figure 我有一张图片,里面有文字

<figure>
    <img src="" alt="">
    <caption><span class="text">TEXT</span></caption>
</figure>

<figure>
    <img src="" alt="">
    <caption><span class="text">TEXT 2</span></caption>
</figure>


<div id="showhere"></div>

I need when I hover an image to display its text inside another div; 当我将图像悬停在另一个div内以显示其文本时,我需要; <div id="showhere"></div> and when mouse is out to hide the text. <div id="showhere"></div>并在鼠标移出时隐藏文本。

I've tried it like this but I'm not very good with jQuery, just starting to learn it.. Can somebody help me out? 我已经试过这样的 ,但我不是很好用jQuery,刚刚开始学习吧..有人可以帮我吗?

here's a fiddle 这是一个小提琴

This should work 这应该工作

$("img").hover(function(){  //Cursor in handler
    $('#showhere').text(
        $(this).next('caption').find('.text').text()
    );
}, function(){              //Cursor out handler
    $('#showhere').text('');
});

try 尝试

$("figure").hover(function() {
   var data=$(this).find(".text").html();
    $("#showhere").hide().stop().html(data).fadeIn();

}, function() {
    $("#showhere").fadeOut();
});

FIDDLE DEMO 现场演示

How about this? 这个怎么样?

$("figure").hover(function() {
    $("#showhere").text($(this).find(".text").text());
}, function() {
    $(this).find(".text").fadeOut();
});

This should also do it: 这也应该这样做:

$(document).ready(function(){
    $("img").hover(
       function () {
      $("#showhere").html( $(this).attr("alt") );
       }, 
       function () {
          ("#showhere").html("");
       }
    );
 });

You get much cleaner code by using the alt attribute: http://jsfiddle.net/dH9yb/ 通过使用alt属性,您可以获得更简洁的代码: http : //jsfiddle.net/dH9yb/

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

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