javascript - 如何优化代码,让我不必每次都写一个新数字?
问题描述
我有这样的js代码:
lightGallery($("#web1")[0], {
selector: 'this',
mobileSettings: {
controls: false,
showCloseIcon: true,
download: false,
}
});
lightGallery($("#web2")[0], {
selector: 'this',
mobileSettings: {
controls: false,
showCloseIcon: true,
download: true,
}
});
lightGallery($("#web3")[0], {
selector: 'this',
mobileSettings: {
controls: false,
showCloseIcon: true,
download: true,
}
});
如何优化代码,让我不必每次都写一个新数字?
解决方案
您可以使用for
循环:
let lowerLimit = 1;
let upperLimit = 3;
for (let i = lowerLimit; i <= uppperLimit; i++) {
lightGallery($("#web" + i)[0], {
selector: 'this',
mobileSettings: {
controls: false,
showCloseIcon: true,
download: false,
}
});
}
推荐阅读
- python - Visual Studio Code Python 加载自己的模块
- python - “客户”对象没有属性“accept_invite”
- node.js - 我应该在每个节点 JS 文件中调用 dotenv 吗?
- webgl - WebGL:无法显示两个纹理
- node.js - 快速中间件中的重定向循环
- reactjs - Next.js 无法预渲染错误页面
- reactjs - 尝试导入组件但出现错误
- netlogo - 定义变量的区别
- ios - 加载 UICollectionView 的 N 个视图并在最后重新加载
- javascript - Angular Portal cdkPortal 指令抛出 Expression Changed 错误