javascript - Show image on link hover inside of table - Stack Overflow

I'm trying to get a scanned image of a receipt stored on the server to display after hovering on a

I'm trying to get a scanned image of a receipt stored on the server to display after hovering on a link inside of a table. I'm not sure what I am doing wrong but was curious if someone could point me in the right direction.

Here is what I have so far

CODE

 jQuery(document).ready(function() {
   $(".preview").hover(function() {
     $(this).closest('img').show();
   }, function() {
     $(this).closest('img').hide();
   });
 });
 .hide-image {
   display: none;
 }
<script src=".11.1/jquery.min.js"></script>
<tr>
  <td>01/14/16</td>
  <td>
    <a href="#" class="preview">Lowes Receipt
          <img src="image-path/image.jpg" class="hide-image" style="z-index: 100; position: absolute;" /></a>
  </td>
  <td>$1235.96</td>
</tr>

I'm trying to get a scanned image of a receipt stored on the server to display after hovering on a link inside of a table. I'm not sure what I am doing wrong but was curious if someone could point me in the right direction.

Here is what I have so far

CODE

 jQuery(document).ready(function() {
   $(".preview").hover(function() {
     $(this).closest('img').show();
   }, function() {
     $(this).closest('img').hide();
   });
 });
 .hide-image {
   display: none;
 }
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<tr>
  <td>01/14/16</td>
  <td>
    <a href="#" class="preview">Lowes Receipt
          <img src="image-path/image.jpg" class="hide-image" style="z-index: 100; position: absolute;" /></a>
  </td>
  <td>$1235.96</td>
</tr>

Share Improve this question edited Jan 26, 2016 at 2:02 dippas 60.7k15 gold badges124 silver badges132 bronze badges asked Jan 26, 2016 at 1:15 kevin3954kevin3954 953 silver badges14 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

Why not only use CSS for this simple task?

.hide-image {
  display: none;
  z-index: 100;
  position: absolute;
}
.preview:hover .hide-image {
  display: block
}
<tr>
  <td>01/14/16</td>
  <td>
    <a href="#" class="preview">Lowes Receipt
      <img src="//lorempixel./100/100" class="hide-image" /></a>
  </td>
  <td>$1235.96</td>
</tr>

$(document).ready(function() {
  $(".preview").hover(function() {
    $(this).find('img').fadeIn();
  }, function() {
    $(this).find('img').fadeOut();
  });
});
.hide-image {
  display: none;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<tr>
  <td>01/14/16</td>
  <td>
    <a href="#" class="preview">Lowes Receipt
    <img src="https://via.placeholder./100" class="hide-image" style="z-index: 100; position: absolute;" />
    </a>
  </td>
  <td>$1235.96</td>
</tr>

You can try this method.

Try next() instead:

$(this).next('img').show();

Try find() instead:

$(this).find('img').show();

Closest moves up the DOM. I have made this mistake before. Use find("img");

Here is a fiddle: https://jsfiddle/unix102/yspbrwkd/

$(document).ready(function () {
    $(".preview").hover(function(){
        $(this).find('img').fadeIn();
     }, function(){
        $(this).find('img').fadeOut();
    });
});

The problem seems to be based on how jQuery's .show() works.

The matched elements will be revealed immediately, with no animation. This is roughly equivalent to calling .css( "display", "block"), except that the display property is restored to whatever it was initially.

Since the display properties initial value is none, show() does nothing.

Here is another way to achieve the same thing while continuing to use jQuery. I also moved some inline styling into the css, for better practice:

HTML

<tr>
   <td>01/14/16</td>
   <td>
      <a href="#" class="preview">Lowes Receipt
      <img src="image-path/image.jpg" class="receipt-image" /></a>
   </td>
   <td>$1235.96</td>
</tr>

CSS

.receipt-image {
    display: none;
    position: absolute;
    z-index: 100; 
}
.receipt-image.visible { 
    display: block;
}

Javascript

jQuery(document).ready(function () {
    $(".preview").hover(function(){
        $(this).closest('.receipt-image').addClass('visible');
    }, function(){
        $(this).closest('.receipt-image').removeClass('visible');
    });
});

However, it's also possible to do this entirely with CSS, no jQuery or Javascript required! Simply remove the JS, and try out this CSS:

CSS

.receipt-image {
    display: none;
    position: absolute;
    z-index: 100; 
}
.preview:hover .receipt-image { 
    display: block;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744969347a4603872.html

相关推荐

  • javascript - Show image on link hover inside of table - Stack Overflow

    I'm trying to get a scanned image of a receipt stored on the server to display after hovering on a

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信