如何将jQuery脚本用于多个分区?

如何将jQuery脚本用于多个分区?,jquery,fade,Jquery,Fade,我希望第一张图片在mouseenter上淡入下一张图片,并在mouseleave上反转。 我想对多个id使用jQuery脚本 <script type="text/javascript"> $('#picone1').mouseenter(function(){ $('#picone1').fadeToggle(500); $('#pictwo1').fadeToggle(500); }); $('#pictwo1').mouseleave(function(){

我希望第一张图片在
mouseenter
上淡入下一张图片,并在
mouseleave
上反转。 我想对多个id使用jQuery脚本

<script type="text/javascript">
$('#picone1').mouseenter(function(){
   $('#picone1').fadeToggle(500);
   $('#pictwo1').fadeToggle(500);
});
$('#pictwo1').mouseleave(function(){
   $('#picone1').fadeToggle(500);
   $('#pictwo1').fadeToggle(500);
});
</script>
<div id="fade">
   <img src="two.gif" width="100" height="100" id="pictwo1" style="display:none;"/>
   <img src="one.gif" width="100" height="100" id="picone1"/>
</div>
<div id="fade">
   <img src="two.gif" width="100" height="100" id="pictwo2" style="display:none;top: 300px;" />
   <img src="one.gif" width="100" height="100" id="picone2" style="top: 300px;" />
</div>
如何仅淡入鼠标进入的
div


是否有其他或更好的脚本来执行此操作?

在jQuery中,在任何事件回调
中,此
引用触发事件的DOM元素。因此,如果你这样做:

 $('#picone1, #picone2').mouseenter(function() {
     // $(this) refers to whichever div was entered, so you can do:
     $(this).fadeToggle(500);
     $(this).prev('img').fadeToggle(500); // get previous IMG in the DOM and fade
 });

我建议您经常这样做,以提供图像类而不是ID,这样您就可以执行类似于
$('img.before')
的操作,并相对地引用另一个img。

在jQuery中的任何事件回调
中,此
引用触发事件的DOM元素。因此,如果你这样做:

 $('#picone1, #picone2').mouseenter(function() {
     // $(this) refers to whichever div was entered, so you can do:
     $(this).fadeToggle(500);
     $(this).prev('img').fadeToggle(500); // get previous IMG in the DOM and fade
 });
我建议您经常这样做,以提供images类而不是ID,这样您就可以执行类似于
$('img.before')
的操作,并相对地引用另一个img。

我尝试了prev()函数,但在第一次转换后,第二次转换将变为空白。我基于prev()函数更新了上面链接上的代码。你能为你所说的类方法发布代码吗?我尝试了prev()函数,但在第一次转换后,第二次转换的代码变为空。我基于prev()函数更新了上面链接上的代码。你能把你说的类方法的代码贴出来吗?