问题
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