似乎应该有一个简单的方法来做到这一点.我正在切换其他一些元素上的类名,并将此更改绑定到该操作.我有i变量集启动淡入淡出,但希望淡出可能存在的其他编号的id.有没有快速的方法
$('#SiteDescriptions' + i).animate({opacity: "1"}, 500);
$('#SiteDescriptions' + !i).animate({opacity: "0"}, 500);
不要使用id并依赖于选择器.而是使用类:
$SD = $('.SiteDescription'); // cache jquery object
$SD.on('click',function(){
// fade out all with this class
$SD.stop().animate({opacity:0},500);
// fade in new active element
$(this).stop().animate({opacity:1},500);
});
如果您尝试选择除该ID之外的任何内容,您将选择页面上不是它的每个元素.而且我认为这不是你想要的.
不要这样做,按类的方式做,但这更接近你的要求:
$('#SiteDescriptions'+i).animate({opacity : 1 },500)
// I don't want to speculate on your dom structure, but if you are using id's
// you still need a way to prevent from fading out everything on the page that
// isn't the new action. So I am assuming that all the #SiteDescriptions are siblings
.siblings().not('#SiteDescriptions'+i).animate({opacity: 0}, 500);
