自定义选择器中的索引不能正常工作

Index in custom selectors not working properly

本文关键字:常工作 工作 不能 选择器 索引 自定义      更新时间:2023-09-26

下面是我定义一个新的自定义选择器组的代码。

(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 ?在这个1.7.2版本中,它返回非零索引,但从1.8.3开始,索引都是零。

在jQuery 1.8及更新版本中,您应该使用新的Sizzle API来创建自定义选择器。

他们给出的例子:

Sizzle.selectors.pseudos.not =
Sizzle.selectors.createPseudo(function( selector /* the argument for not */ ) {
    var matcher = Sizzle.compile( selector );
    return function( elem ) {
        return !matcher( elem );
    };
});