这是我定义新的自定义选择器组的代码。
(function($) {
$.extend($.expr[':'], {
group: function(element, index, matches, set) {
var num = parseInt(matches[3], 10);
if (isNaN(num)) {
return false;
}
console.log('The value of index element is '+index);
return index%(num * 2)<num;
}
});
})(jQuery);
然后在我的 document.ready(function(){ });
当我调用一个简单的
$('tr:group(3)')
console.log 的内容如下 -->
`83` The value of index element is 0
这意味着索引始终为 0,我不知道为什么。任何帮助将不胜感激。
请检查以下FIDDLE http://jsfiddle.net/hardeepmehta/KkTQX/
最佳答案
您运行的是哪个版本的 jQuery?在 this fiddle with 1.7.2 ,它返回非零索引,但是 starting with 1.8.3 ,索引全部为零。
在 jQuery 1.8 及更高版本中,您应该使用 new Sizzle API for creating custom selectors .
他们给出的例子:
Sizzle.selectors.pseudos.not =
Sizzle.selectors.createPseudo(function( selector /* the argument for not */ ) {
var matcher = Sizzle.compile( selector );
return function( elem ) {
return !matcher( elem );
};
});
关于javascript - 自定义选择器中的索引无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23569914/