简单的增量方式

olo*_*olo 3 javascript jquery

$("a[rel=example_1],a[rel=example_2],a[rel=example_3],a[rel=example_4],a[rel=example_5],a[rel=example_6],a[rel=example_7],a[rel=example_8],a[rel=example_9],a[rel=example_10]").fancybox({............
Run Code Online (Sandbox Code Playgroud)

你好,现在我必须手动输入这些代码.

有没有简单的方法来增加数字?

我知道我可以使用类似的东西a[rel=example_[1-10]].什么是正确的语法?

非常感谢

sac*_*een 8

您可以使用属性启动选择器

a[rel^="example_"]
Run Code Online (Sandbox Code Playgroud)

这将选择a具有rel以"example_"开头的属性的所有元素.如果要选择与该模式匹配的所有元素,我建议使用此循环.


zer*_*kms 5

如果它对你没问题 - 你可以选择

$('a[rel^="example_"]').
Run Code Online (Sandbox Code Playgroud)

它将匹配所有a已经rel开始的example_

否则 - 使用精细过滤:

$('a[rel^="example_"]').filter(function() {
    var match = $(this).prop('rel').match(/^example_(\d+)$/);
    return match.length == 2 && parseInt(match[1]) <= 10;
})
Run Code Online (Sandbox Code Playgroud)

.filter()显式检查1..10下划线后是否有数字的回调