jquery $.expr[':'] 用法体会
来源:互联网 发布:淘宝网被冻结 编辑:程序博客网 时间:2024/05/16 09:41
今天在看$.widget 源码时发现有这样一行代码:
读到这些代码有些迷茫,于查百度发现有一个用法
能过些代码,体会到了代码作用,其实就是一个伪类选择器,记录一下,留作备忘。
另外可参阅:http://answers.oreilly.com/topic/1055-creating-a-custom-filter-selector-with-jquery/
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, like so:
The function will be run on all elements in the current collection and needs to return true (to keep the element in the collection) or false (to remove the element from the collection). Three bits of information are passed to this function: the element in question, the index of this element among the entire collection, and a match array returned from a regular expression match that contains important information for the more complex expressions.
For example, you might want to target all elements that have a certain property. This filter matches all elements that are displayed inline:
Now that we have created a custom selector, we can use it in any selector expression:jQuery’s custom selectors (
:radio
,:hidden
, etc.) are created in this way.As mentioned, the third parameter passed to your filter function is an array returned from a regular expression match that jQuery performs on the selector string. This match is especially useful if you want to create a filter expression that accepts parameters. Let’s say that we want to create a selector that queries for data held by jQuery:
The purpose of the selector would be to select all elements that have had data attached to them via jQuery’s
data()
method—it specifically targets elements with a datakey ofsomething
, equal to the number 123.The proposed filter (
:data
) could be created as follows:
The reason for such a complex regular expression is that we want to make it as flexible as possible. The new selector can be used in a number of different ways:
Now we have a totally new way of querying data held by jQuery on an element.
If you ever want to add more than one new selector at the same time, it’s best to use jQuery’s
extend()
method:
转自:blog.163.com/jinwei_zhiyuan/blog/static/11582265201312114147797
- jquery $.expr[':'] 用法体会
- jquery $.expr[':'] 用法体会
- expr用法
- expr用法
- expr用法
- Linux expr用法
- expr的用法
- Shell expr的用法
- expr常见用法
- expr的用法总结
- Shell expr的用法
- expr命令用法-实例讲解
- expr命令用法-实例讲解
- shell中expr的用法
- expr命令参数及用法
- shell - 关于expr的用法
- expr
- expr
- 浅析MVC框架中View层的优雅设计及实例
- 【Leetcode】1. Two Sum
- AngularJS指令学习和理解
- JAVA中静态块、静态变量加载顺序详解
- 【51 nod 1294】 修改数组
- jquery $.expr[':'] 用法体会
- java常用的几种线程池比较
- DAO模式
- 13.服务器端脚本
- 在C语言中使用全局变量
- ExtJs实现阻止冒泡,以及再次允许冒泡——stopPropagation的正确使用
- SSL JudgeOnline 2322——运输
- WebRTC中丢包重传NACK实现分析
- WindowsUpdate检查更新报错,错误码8024402F