jquery custom Selector jquery Customize Selector

Source: Internet
Author: User

What is a jquery selector, see selector:http://docs.jquery.com/selectors in jquery

such as $ ("Div:contains (' John ')"). CSS ("text-decoration", "Underline"), contains selectors, etc.

Basic syntax for the jquery custom selector:

$.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!!! Used to store parameter values, see Custom selectors with parameters.
Stack-stack of all elements to loops

Return true to include the current element
Return false to explude the current element
*/
};

Usage:

$ ('. Someclasses:test '). dosomething ();
1. Create a simple custom selector (returns an element with a "rel" label that is not empty)

$.expr[': '].withrel = function (obj) {

var $this = $ (obj);

Return ($this. attr (' rel ')! = ');

};

Application:

$ (' A:withrel '). CSS (' background-color ', ' yellow ');

Effect See: http://custom-drupal.com/jquery-demo/custom-selectors/

2. Create a custom selector with parameters, implemented by Meta.
Grammar:
$ (' a:test (argument) ');

The format of Meta would carry the following Info:meta is as follows (Meta[3] corresponding value is 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

]

Instance:


$.expr[': '].withattr = function (obj, index, meta, stack) {

Return ($ (obj). attr (meta[3])! = ");

};

Application:
$ (' a:withattr (rel) '). CSS (' background-color ', ' yellow ');

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.