本文介绍了自定义jQuery伪选择器接收未定义的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据我发现的几个不同的教程,为jQuery添加一个自定义伪选择器,目前正在使用v1.8.0。我基本上试图实现不区分大小写:包含选择器。

I'm attempting to add a custom pseudo-selector to jQuery, currently using v1.8.0, based on a few different tutorials I've found. I'm essentially trying to implement a case insensitive :contains selector.

我目前的化身看起来像这样

My current incarnation looks like this

$.expr[':'].icontains = function(obj, index, meta, stack){
     return (obj.textContent || obj.innerText || jQuery(obj).text() || '').toLowerCase().indexOf(meta[3].toLowerCase()) >= 0;
};

来自。看起来选择器正在初始化,但是当jQuery调用函数时,只定义了obj参数。其余3个参数未定义。

which came from this post. It seems like the selector is getting initialized correctly, but when jQuery calls the function only the obj parameter is defined. The remaining 3 arguments are coming in undefined.

当我将参数记录到控制台时,我看到一个包含2个项目的数组,第一个项目是DOM对象我的选择器在:icontains调用之前返回,第二个未定义。

When I log the arguments to the console I'm seeing an array of 2 items, with the first item being the DOM object returned by my selector prior to the :icontains call, and the second being undefined.

有没有人知道为什么会发生这种情况?

Does anyone have an idea as to why this would be happening?

推荐答案

他们在1.8中重写了Sizzle。目前,的方式如下:。

They did a rewrite for Sizzle in 1.8. Currently, the way of defining a pseudo is as follows: http://jsfiddle.net/bazWj/.

$.expr.pseudos.icontains = $.expr.createPseudo(function(arg) {
    return function(elem) {
         return (elem.textContent
                  || elem.innerText
                  || jQuery(elem).text()
                  || '')
        .toLowerCase()
        .indexOf(arg.toLowerCase()) >= 0;

    };
});

这篇关于自定义jQuery伪选择器接收未定义的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 07:35