关于jquery $.expr[':']

学习JQuery时 遇到一组代码
jQuery.expr[':'].data = function(a, i, m){
...
};  

对这个$.expr[':'] 实在没搞懂是啥,四处搜寻了一下,找到一些相关解释:

If you need a reusable filter to target specific elements based on their characteristics, you can extend jQuery’s selector expressions under the jQuery.expr[':'] object; this is an alias for Sizzle.selectors.filters. Each new filter expression is defined as a property of this object。意思为:如果你需要一个可重用的过滤器来根据它们的特性来定位特定的元素,你可以在jQuery.expr [':']对象下扩展jQuery的选择器表达式;这是Sizzle.selectors.filters的别名。每个新的过滤器表达式都被定义为这个对象的属性。

实际上说白了就是自定义伪类选择器 类似于

jQuery.expr[':'].between = function(a, i, m){
 var tmp=m[3].split(".");
return tmp[0]-0<i&&i<tmp[1]-0;
};  

定义了一个between选择器。

另外,想要同时添加多个新选择器,最好使用jQuery的extend()方法

jQuery.extend(jQuery.expr[':'], {

       data1 : function(){

        

        },

      data2 : function(){

        

        },
  data3 : function(){

        

        },

}); 

猜你喜欢

转载自blog.csdn.net/Q846169253/article/details/79964201