如何在这种情况下使用jquery?

jes*_*ess 0 jquery

我有以下场景 - 我正在显示产品名称列表作为链接.现在,当用户将鼠标悬停在链接上时,我需要显示产品的图像(图像被隐藏).html就像这样(动态构建,所以prod1,prod2等)

 <a id="prod1" title="product"></a>
 <div class=hidden><img src=""></img></div> 
Run Code Online (Sandbox Code Playgroud)

Rei*_*gel 5

你可以使用.show().hide().

如果可以的话,给链接一个共同的类,这样你就可以这样做......

$('.prod').hover(
  function{
    $(this).next('.hidden').show();
  },
  function{
    $(this).next('.hidden').hide();
  }
);
Run Code Online (Sandbox Code Playgroud)

但如果你不能改变html,你可以这样做,

function over(){
    $(this).next('.hidden').show();
}
function out(){
    $(this).next('.hidden').hide();
}
$('#prod1,#prod2').hover(over,out); // this will show on mouseover and hide on mouseout

// but if you just want to show and not hide do this
$('#prod1,#prod2').hover(over);
Run Code Online (Sandbox Code Playgroud)