【问题标题】:how to use jquery variables in html如何在html中使用jquery变量
【发布时间】:2010-12-24 18:41:32
【问题描述】:

我正在使用 jquery,我想要在这里做的是能够设置将根据悬停的链接显示的图像。

<script type='text/javascript'>
$(function(){
  $('img').hide();
  $('a').mouseenter(function(){
    var currentimg= $(this).html();
    alert(currentimg);
    $("img[src=currentimg.jpg]").show(); //I want to use currentimg variable here for the name of the jpg file
  });

  $('a').mouseleave(function(){
    $('img').hide();
  });
});
</script>

【问题讨论】:

    标签: javascript jquery html image


    【解决方案1】:

    你可以把字符串串联起来使用,例如:

    $("img[src='" + currentimg +"']").show();
    

    请注意,.mouseenter(handler).mouseleave(handler) 也有一个 .hover() 快捷方式,如下所示:

    $(function(){
      $('img').hide();
      $('a').hover(function(){
         var currentimg = $(this).html();
         $("img[src='" + currentimg +"']").show();
      }, function(){
         $('img').hide();
      });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      • 2014-03-01
      • 1970-01-01
      • 2020-06-30
      相关资源
      最近更新 更多