0

I have the following code which when I hover over a hyperlink, I like to show the image of it. The problem is that no image comes up.

Here is what it looks like

      $(document).ready(function () {
      $('a').hover(function (e) {

          var href = $(this).attr('href');
          alert(href); // shows c:/images/jj.gif for that particular record as I have the hyperlinks for a given column within a grid

          $("body").append("<p id='preview'><img src='" + this.href + "' alt='Image preview' /></p>");


      });
  });
4

2 回答 2

1

这是更新的小提琴:http: //jsfiddle.net/w5jLd/1/

$(document).ready(function() {
    $('a').hover(function(e) {

        var href = $(e.target).attr('href');
        $(e.target).next('div').append("<p id='preview'><img src='" + href + "' alt='Image preview' /></p>");

    },function(){   // i have added this when mouse out of the link 
                    // preview will be destroyed.

        $('#preview').remove();

    });
});

您悬停在链接上,但没有捕获目标本身。所以e.target选择悬停项目。

于 2012-11-26T18:44:53.257 回答
1
$(document).ready(function () {
      $('a').hover(function (e) {

          var href = $(this).attr('href');
          alert(href); // shows c:/images/jj.gif for that particular record as I have the hyperlinks for a given column within a grid

          $("body").append("<p id='preview'><img src='" + href + "' alt='Image preview' /></p>");


      });
  });

如果您已经将 href 分配给变量,则不需要将其引用为 this.href

于 2012-11-26T18:25:06.347 回答