我使用以下代码为提到的DIV调用JS函数 #lightGallery
$("#lightGallery").lightGallery({
thumbnail: false,
});
我需要修改JS代码以使函数被调用为任何 #DIV+num(如 #lightGallery1)调用,#lightGallery2,...等
$('[id^="lightGallery"]').lightGallery({
thumbnail: false,
});
取而代之的是,我会简单地给元素一个类名,并用
$('.lightGallery').lightGallery({
thumbnail: false,
});
最简单的是,对id
使用属性开始选择器:
$("[id^=lightGallery]").lightGallery({
thumbnail: false,
});
如果需要过滤掉id
以 lightGallery
开头但后跟非数字字符的其他元素,也可以使用 filter()
:
$("[id^=lightGallery]").filter(function(){
return /^lightGalleryd+/.test(this.id);
}).lightGallery({
thumbnail: false,
});
引用:
- .CSS:
- 属性开头为 (
[attribute^=value]
) 选择器。
- 属性开头为 (
- JavaScript:
- JavaScript 正则表达式。
-
RegExp.prototype.test()
.
- j查询:
-
filter()
.
-