gpt4 book ai didi

jQuery - 有没有办法重用参数来减少代码重复?

转载 作者:行者123 更新时间:2023-12-01 00:46:09 25 4
gpt4 key购买 nike

假设我有一些这样的代码:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com