Jquery:将鼠标悬停在<a> tag</a>上时显示图片和文字

时间:2012-10-23 04:12:44

标签: javascript jquery

我目前正在使用jquery和<a>标记。现在,每当我将鼠标悬停在标签上时,我就可以显示图像。我正在努力实现在图片下面与右<a>标签对应的实际显示文字的部分。如何在鼠标悬停期间在每张图片下方显示相应的<legend>?一次显示一个图例以及匹配的图片。这是我的EXAMPLE

代码Jquery

<script>
$(document).ready(function () {
var $images = $("div#images");
var $currentImage = $images.children("img#cheeseburger");

$currentImage .show();

$("div#links > a").mouseenter(function() {
    var ID = $(this).data("content");
    var $image = $images.children("img#" + ID);

    if (!$image.is($currentImage)) {
        $currentImage.hide();
    }

    $currentImage = $image;
    $image.stop(true, true).fadeIn("slow");
});

});
</script>

HTML

<div id="links">
    <h2> Select A Category </h2> 
    <a href="example.htm" class="large magenta awesome" data-content="cheeseburger">Cheeseburger »</a>
    <a href="example.htm" class="large blue awesome" data-content="tacos">Tacos »</a>
    <a href="example.htm" class="large red awesome" data-content="salads">Salads »</a>
</div>
<center>
    <br />
    <div id="images">
        <img src="images/cheeseburger.jpg" id="cheeseburger">
        <img src="images/tacos.jpg" id="tacos">
        <img src="images/salad.jpg" id="salads" >
    </div>      
</center>

<fieldset class="el05">
        <legend style="font-size:15px;"><b>Cheeseburger:</b></legend>
        <p> Example one</p>
</fieldset>

<fieldset class="el05">
        <legend style="font-size:15px;"><b>Tacos:</b></legend>
        <p> Example two.</p>
</fieldset>

<fieldset class="el05">
        <legend style="font-size:15px;"><b>Salad:</b></legend>
        <p> Example three.</p>
</fieldset>

2 个答案:

答案 0 :(得分:1)

尝试我刚刚在一个

上实现的类似内容
 <fieldset class="cheeseburger">
    <legend style="font-size:15px;"><b>Cheeseburger:</b></legend>
    <p> Example one</p>
</fieldset>


<script>
 $(document).ready(function () {
 var $images = $("div#images");
 var $currentfieldset = $(".cheeseburger");
 var $currentImage = $images.children("img#cheeseburger");

 $currentImage .show();

  $("div#links > a").mouseenter(function() {
   var ID = $(this).data("content");
   var $image = $images.children("img#" + ID);
    var $fieldset = $('.' + ID);

   if (!$image.is($currentImage)) {
      $currentImage.hide();
   }
   if (!$fieldset .is($currentfieldset)) {
      $currentfieldset.hide();
   }

   $currentImage = $image;
  $fieldset.show();
  $currentfieldset= $fieldset ;
   $image.stop(true, true).fadeIn("slow");
 });

 });
 </script>

答案 1 :(得分:0)

请试试这个:

Jquery的:

<script type="text/javascript">
$(document).ready(function () {
   var $images = $("div#images");
   var $currentfieldset = $(".cheeseburger");
   var $currentImage = $images.children("img#cheeseburger");

   $currentImage .show();

   $("div#links > a").mouseenter(function() {
   var ID = $(this).data("content");
   var $image = $images.children("img#" + ID);
   var $fieldset = $('.' + ID);

   if (!$image.is($currentImage)) {
      $currentImage.hide();
   }
   if (!$fieldset .is($currentfieldset)) {
      $currentfieldset.hide();
   }

   $currentImage = $image;
   $currentfieldset= $fieldset ;
   $image.stop(true, true).fadeIn("slow");
   $currentfieldset.stop(true,true).fadeIn("slow") ;
   });

});
</script>

HTML

<div id="links">
    <h2> Select A Category </h2> 
    <a href="example.htm" class="large magenta awesome" data-content="cheeseburger">Cheeseburger »</a>
    <a href="example.htm" class="large blue awesome" data-content="tacos">Tacos »</a>
    <a href="example.htm" class="large red awesome" data-content="salads">Salads »</a>
</div>
<center>
    <br />
    <div id="images">
        <img src="images/cheeseburger.jpg" id="cheeseburger">
        <img src="images/tacos.jpg" id="tacos">
        <img src="images/salad.jpg" id="salads" >
    </div>      
</center>

<fieldset class="el05 cheeseburger">
        <legend style="font-size:15px;"><b>Cheeseburger:</b></legend>
        <p> Example one</p>
</fieldset>

<fieldset class="el05 tacos" style="display:none;">
        <legend style="font-size:15px;"><b>Tacos:</b></legend>
        <p> Example two.</p>
</fieldset>

<fieldset class="el05 salads" style="display:none;">
        <legend style="font-size:15px;"><b>Salad:</b></legend>
        <p> Example three.</p>