这就是我最终做的事情,并且按照我的意愿工作。可能有一种方法可以简化这一点,我愿意接受建议。感谢那些回答的人。
jQuery(document).ready(function($) {
//rollover swap images with rel
var img_src = "";
var new_src = "";
$('.rollover').click(function() {
if($(this).hasClass("clicked"))
{
//do nothing if this element has already been clicked
}
else
{
//Unbind the hover effect from this element
$(this).removeClass("rollover");
$(this).unbind('mouseenter').unbind('mouseleave')
//Go through and find anything having 'clicked' class -
//in theory, there should only be one element that has the 'clicked' class at any given time.
$('.clicked').each(function (){
$(this).removeClass("clicked");
$(this).addClass("rollover");
//Swap the images
img_src = $(this).attr('src'); //grab original image
new_src = $(this).attr('rel'); //grab rollover image
$(this).attr('src', new_src); //swap images
$(this).attr('rel', img_src); //swap images
//Rebind hover (since we unbound it back up there ^^) to this *specific* element.
//If you do $(".rollover") to rebind, it kills all the other existing binds and binds ONLY this element.
$(this).hover(function(){
img_src = $(this).attr('src'); //grab original image
new_src = $(this).attr('rel'); //grab rollover image
$(this).attr('src', new_src); //swap images
$(this).attr('rel', img_src); //swap images
});
});
$(this).addClass("clicked");
}
});
$(".rollover").hover(function(){
img_src = $(this).attr('src'); //grab original image
new_src = $(this).attr('rel'); //grab rollover image
$(this).attr('src', new_src); //swap images
$(this).attr('rel', img_src); //swap images
});
});