惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
G
Google Developers Blog
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
J
Java Code Geeks
P
Proofpoint News Feed
V
Visual Studio Blog
爱范儿
爱范儿
The Cloudflare Blog
博客园 - 叶小钗
V
V2EX
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
M
MIT News - Artificial intelligence
Microsoft Security Blog
Microsoft Security Blog
博客园 - 聂微东
H
Help Net Security
B
Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 【当耐特】
量子位
宝玉的分享
宝玉的分享
WordPress大学
WordPress大学
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

博客园 - leegool

常见XSD问题 EntitySpace 常用语句 抗投诉空间 C#用WebClient下载File时操作超时的问题 用C# 实现 Zen Cart 的用户密码加密算法 ASP.NET MVC 3 新特性 ASP.NET MVC 局部缓存实现 用户控件缓存 Partial Output Caching - leegool System.Web.Security.SqlMembershipProvider”要求一个与架构版本“1”兼容的数据库架构。 关于MarshalByRefObject的解释 MVC upload image MVC上传图片的例子 - leegool 清楚浏览器缓存 Overview of Full Text Stop Words(MSSQL全文索引的干扰词概括)MSSQL 全文索引的最小单词长度 JQuery性能优化 MS SQL 事务隔离级别 认识ASP.NET 中的 AppDomain 国外服务器上 中文成 显示乱码 Sharing cookie Across domain 跨域cookie访问 cookie跨域 web性能优化(最佳) EntitySpaces 2009 Trial Expire 异常
jQuery Custom Selector JQuery自定义选择器 - leegool
leegool · 2009-11-22 · via 博客园 - leegool

 什么是JQuery的选择器呢,详见JQuery中的Selector: http://docs.jquery.com/Selectors

比如 $("div:contains('John')").css("text-decoration", "underline");的contains选择器等等

JQuery自定义选择器的基本语法:

$.expr[':'].test = function(obj, index, meta, stack){
   
/* obj - is a current DOM element
       index - the current loop index in stack
       meta - meta data about your selector !!! 用来存参数值,详见带参数的自定义选择器。
       stack - stack of all elements to loop
   
       Return true to include current element
       Return false to explude current element
    */

};

// Usage:
$
('.someClasses:test').doSomething();
1.创建一个简单的自定义选择器(将返回"rel"标签不为空的元素)

$.expr[':'].withRel = function(obj){
 
var $this = $(obj);
 
return ($this.attr('rel') != '');
};
// 应用:
$
('a:withRel').css('background-color', 'yellow');
效果见:http://custom-drupal.com/jquery-demo/custom-selectors/
 
2. 创建带参数的自定义选择器,通过meta来实现.
语法:
$('a:test(argument)');
//meta would carry the following info: meta的格式如下(meta[3]对应的值是argument)
[
   
':test(argument)', // full selector
   
'test',            // only selector
   
'',                // quotes used
   
'argument'         // parameters
]

$

('a:test("arg1, arg2")');
//meta would carry the following info:
[
   
':test('arg1, arg2')', // full selector
   
'test',                // only selector
   
'"',                   // quotes used
   
'arg1, arg2'           // parameters
]
 
实例:
$.expr[':'].withAttr = function(obj, index, meta, stack){
 
return ($(obj).attr(meta[3]) != '');
};
应用:
$('a:withAttr(rel)').css('background-color', 'yellow');

 参考地址:

http://jquery-howto.blogspot.com/2009/06/jquery-custom-selectors-with-parameters.html

http://jquery-howto.blogspot.com/2009/06/custom-jquery-selectors.html

http://www.softwareunity.com/jquery/JQueryMoreSelectors/