Custom jQuery Pseudo-selector Receiving Undefined Arguments

五迷三道 提交于 2019-12-13 14:28:09

问题


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;
};

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.

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:


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;

    };
});


来源:https://stackoverflow.com/questions/12099325/custom-jquery-pseudo-selector-receiving-undefined-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!