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

标签 jquery variables parameters reusability

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

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/

相关文章:

Java 使用带有列表和接口(interface)的泛型

c++ - 按值传递或按引用传递

c# - 将委托(delegate)与参数一起传递给函数

python - 无法在 TensorFlow 中共享/重命名变量

通过ajax调用c#方法时出现Javascript错误404

javascript - 为什么这个变量会出现 "Uncaught TypeError: Cannot read property"错误?

javascript - 我的 JS 只有我的第一个事件正在触发

javascript - 创建 Stripe token 时遇到问题 : 402 (Payment Required) error

linux - gnuplot 在文件路径中有一个变量

variables - BAT 文件 : variable contents as part of another variable