如何在html中使用jquery变量

use*_*269 1 html javascript jquery image

我正在使用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>
Run Code Online (Sandbox Code Playgroud)

Nic*_*ver 6

您可以连接字符串以供使用,例如:

$("img[src='" + currentimg +"']").show();
Run Code Online (Sandbox Code Playgroud)

需要注意的是,还有一个.hover()快捷方式.mouseenter(handler).mouseleave(handler),如下所示:

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