Customization selector written by jquery plugin

Source: Internet
Author: User

Many people love it because of the powerful choice of jquery (yes, I hate native JS Findelementbyid), but do not want to put some often used in the chain-type operation of a selector it?!

In terms of mechanism, jquery's selector parser first uses a set of regular expressions to parse the selector, then executes the selector function on each parsed selector, and finally determines whether the element is preserved based on true or false.

For example:

$ (' div:gt (1) ')

In the JQuery source file is the jquery.expr[":"] = JQuery.expr.pseudos object to maintain the selector, so we extend the time, that is, to extend the object.

"GT": Createpositionalpseudo (function(matchindexes, length, argument) {            var i = Argument < 0? Argument + length:argument;              for (; ++i < length;) {                Matchindexes.push (i);            }             return matchindexes;}

Matchindexes is the DOM element that needs to be returned, length is the total number of elements in the DOM, and argument is the parameter in the GT ().

As you can see in the code I increments from argument+1 until the total number of elements. All of these index will be selected, and then return back.

functionCreatepositionalpseudo (FN) {returnMarkfunction (function(argument) {argument= +argument; returnMarkfunction (function(seed, matches) {varJ, Matchindexes=fn ([], seed.length, argument), I=matchindexes.length; //Match elements found at the specified indexes             while(i-- ) {                if(seed[(j =Matchindexes[i]) ) {Seed[j]= ! (Matches[j] =Seed[j]);    }            }        }); });}

The selector's work is done! (Can not understand the source code is OK, next is the key )

What if we were to write a between selector for ourselves? Use the (a,i,m) parameter to pass in

A: The DOM element that is currently traversed

I: The DOM element currently traversed to index, starting from 0

M: The result of the regular parsing, m[0]: ": GT (1)", M[1]: ":", m[2]: "GT (1)", M[3]: "1" (so m[3] is the incoming parameter of our custom selector! )

;(function   ($) {$.extend ($.expr[ " : "],{//  Here we use extend to extend the object of jquery.expr[": "]   between:  function   (a,i,m) { var  Tmp=m[3].split (","); //       separated by commas, cut into an array of     return  tmp[0]-0<i&&i<tmp[1]-0; }  });}) (jQuery);  //  plug-in application  $ ( function   () {$ ( div:between (2,10)). CSS ("Color", "Red"  

Selector plug-ins are obscure, involving internal parsing engine, need to carefully analyze the source code of jquery to understand the mystery.

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.