標籤:http color os io cti ar div line
什麼是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‘);