jQuery选择性悬停

jQuery选择性悬停,jquery,hover,selection,fadein,Jquery,Hover,Selection,Fadein,我试图用jQuery做一个简单的任务:我有一个单词列表,当鼠标悬停时,这些单词应该在相应的图像中消失。例如: <a href="#" class="yellow">Yellow</a> <a href="#" class="blue">Blue</a> <a href="#" class="green">Green</a> <img src="yellow.jpg" class="yellow"> <im

我试图用jQuery做一个简单的任务:我有一个单词列表,当鼠标悬停时,这些单词应该在相应的图像中消失。例如:

<a href="#" class="yellow">Yellow</a>
<a href="#" class="blue">Blue</a>
<a href="#" class="green">Green</a>

<img src="yellow.jpg" class="yellow">
<img src="blue.jpg" class="blue">
<img src="green.jpg" class="green">
上面的方法很好,但我还在学习,我想有更好的方法来实现这一点,而不是重复函数

有人能给我点光吗?如何改进此代码

<a href="#" class="yellow" data-type="color">Yellow</a>
<a href="#" class="blue" data-type="color">Blue</a>
<a href="#" class="green" data-type="color">Green</a>
我想你应该试试这个。我使用数据作为自定义属性的前缀,因为它符合html5。如果你愿意,你可以说点什么

通常,您可能不需要使用数据颜色自定义属性,但因为我认为要使其更通用,所以我使用了该属性。您也可以编写这样的代码:

<a href="#" class="yellow">Yellow</a>
<a href="#" class="blue">Blue</a>
<a href="#" class="green">Green</a>
但是通过这种方式,您应该确保所有链接都是与图像相关的链接

<a href="#" id="yellow" class="colorLink">Yellow</a>
<a href="#" id="blue" class="colorLink">Blue</a>
<a href="#" id="green" class="colorLink">Green</a>

<img src="yellow.jpg" class="yellow">
<img src="blue.jpg" class="blue">
<img src="green.jpg" class="green">

$("a.colorLink").hover(
    function(){
        $("img."+this.id).fadeIn('fast');
    },
    function(){
        $("img."+this.id).fadeOut('fast');
    }
);
我想你应该试试这个。我使用数据作为自定义属性的前缀,因为它符合html5。如果你愿意,你可以说点什么

通常,您可能不需要使用数据颜色自定义属性,但因为我认为要使其更通用,所以我使用了该属性。您也可以编写这样的代码:

<a href="#" class="yellow">Yellow</a>
<a href="#" class="blue">Blue</a>
<a href="#" class="green">Green</a>
但是通过这种方式,您应该确保所有链接都是与图像相关的链接

<a href="#" id="yellow" class="colorLink">Yellow</a>
<a href="#" id="blue" class="colorLink">Blue</a>
<a href="#" id="green" class="colorLink">Green</a>

<img src="yellow.jpg" class="yellow">
<img src="blue.jpg" class="blue">
<img src="green.jpg" class="green">

$("a.colorLink").hover(
    function(){
        $("img."+this.id).fadeIn('fast');
    },
    function(){
        $("img."+this.id).fadeOut('fast');
    }
);
这将为对应于图像的每个链接设置唯一的id

这将为对应于图像的每个链接设置唯一的id

<a href="#" id="yellow" class="colorLink">Yellow</a>
<a href="#" id="blue" class="colorLink">Blue</a>
<a href="#" id="green" class="colorLink">Green</a>

<img src="yellow.jpg" class="yellow">
<img src="blue.jpg" class="blue">
<img src="green.jpg" class="green">

$("a.colorLink").hover(
    function(){
        $("img."+this.id).fadeIn('fast');
    },
    function(){
        $("img."+this.id).fadeOut('fast');
    }
);