jquery - Making div element dynamic with another function
问题描述
I have a block of code that I want to try and refactor.
$(function () {
$('.dots:nth-of-type(1)').click(function () {
currentSlide(1);
});
$('.dots:nth-of-type(2)').click(function () {
currentSlide(2);
});
});
How can I make the nth-of-type(1) dynamic along with the currentSlide(1) so that for each nth-of-type(), I just need to click on it in order currentSlide to be the same?
解决方案
使用 jQuery 的index
方法:
$(function() {
$('.dots').click(function() {
currentSlide($(this).index());
});
});
推荐阅读
- nativescript - Nativescript RadDataForm - 从后面的代码中提供 valuesProvider
- android - variant.getJavaCompiler()、variant.getJavaCompileProvider()、variantOutput.getPackageLibrary()、variant.getPackageLibraryProvider() 中的警告
- angular - 已修复:Angular 9:使用 Angular 功能的未修饰基类
- ruby-on-rails - 是什么导致 bin/rails:与非类/模块(TypeError)相比?
- r - 在 R 中查找正则表达式调用以仅返回名字和姓氏或完整的团队名称
- java - 如何在多逗号行中使用逗号分隔字符串
- node.js - 将节点版本 8 更新到 10 时如何获得所有兼容的软件包版本?
- ios - 我们可以为 iPhone 和 Apple Watch 应用程序使用相同的代码库吗?
- azure - 在 Log Analytics 中使用共享的用户定义 Kusto 函数
- rust - 在 FFI 中使用 () (和其他零大小类型)