我从this post at CSSTricks.com的评论中抓取了以下代码段。
$.extend($.expr[":"], {
"containsNC": function(elem, i, match, array) {
return (elem.textContent || elem.innerText || "").toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
}
});
基本上,它提供了一个伪选择器:containsNC
,它的工作方式与:contains
类似,但不区分大小写。
我希望能够检测到此选择器是否可用,以便我可以有条件地使用:contains
作为后备。
如何检查是否存在此类自定义选择器?
或者,是否有更好的方法在较新的jQuery中进行不区分大小写的匹配?
供参考,以下是我目前正在使用:contains
(过滤Facebook好友列表)的方法
$('#friend-search').on('keyup', 'input', function() {
var $search = $(this).val(),
$friends = $('.friend'),
match = ':contains(' + $search + ')';
$friends.hide().filter(match).show();
});
答案 0 :(得分:1)
在发布这个问题之后,我做了一些讨论,很容易找到答案:
var containsNC_isDefined = typeof $.expr[":"].containsNC === "function";
if (containsNC_isDefined) alert(":containsNC exists!");