假设我有一些这样的代码:
jQuery('#retouching-image-1').beforeAfter({
animateIntro: true,
introDelay: 500
});
jQuery('#retouching-image-2').beforeAfter({
animateIntro: true,
introDelay: 500
});
不是每次需要时都重复 animateIntro: true, introDelay: 500
,是否可以将这些值放入某种可重复使用的变量中?
谢谢。
最佳答案
尝试以下操作
var x = {
animateIntro: true,
introDelay: 500
};
jQuery('#retouching-image-1').beforeAfter(x);
jQuery('#retouching-image-2').beforeAfter(x);
另一个可能更可重用的选项是使用类而不是 id 来标记这些元素。假设您向其中每一项添加了“retouchImage”类。然后您可以将代码简化为以下内容
jQuery('.retouchImage').beforeAfter({
animateIntro: true,
introDelay: 500
});
关于jQuery - 有没有办法重用参数来减少代码重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7531549/