jquery 1.9.1 Source Analysis Series (14) Common jquery tools _jquery

Source: Internet
Author: User
Tags arrays extend instance method numeric value object object trim jquery library javascript eval

To prepare for the next chapter to analyze the animation process, look at some of the tools first. Where the queue tool is used frequently in animation processing.

Jquery.fn. Queue ([QueueName] [, Newqueue]) | | ([QueueName,] callback) (Gets or sets the function queue to be executed on the current matching element. If the current jquery object matches more than one element: When the queue is fetched, only the queue on the first matching element is fetched; When the queue is set (replace queue, append function), each matching element is set separately. If you need to remove and execute the first function in the queue, use the Dequeue () function. You can also use the Clearqueue () function to empty the specified queue.

Jquery.fn. Dequeue ([dequeuename]) removes the first function in the specified queue for each matching element and performs the removed function. You can also use the Clearqueue () function to empty the specified queue (the function is not executed))

Jquery.fn. Clearqueue ([Dequeuename]) (empty all outstanding items in the specified queue for each matching element)

Jquery.error (msg) throws an exception containing the specified string information. )

Jquery.each (object, callback) iterates through the specified objects and arrays and iterates through the specified function as a context for each property of the object (or each member of the array). The so-called context, meaning that the this pointer inside the function references the element. This function belongs to the global jquery object. Note that this is different from each () function of the jquery object (instance), but the each () implementation of the JQuery object (instance) is also called Jquery.each.

Jquery.proxy () (Changes the context of the function). You can pass the specified function to the function, which returns a new function whose execution code is unchanged, but the context within the function (this) has been changed to the specified value

  Usage One:

Jquery.proxy (function, context [, additionalarguments])

Changes the context object of function functions to the specified contexts.

Usage Two:

Jquery.proxy (context, name [, Additionalarguments])

Changes the context of a function named name to the specified contexts. The function name should be a property of the context object.

Jquery.map (object, callback) (uses the specified function to handle each element in the array (or each property of an object) and encapsulates the processing result as a new array return. The jquery library also has an instance method JQuery.fn.map () with the same name, which is used only to traverse the elements that match the current jquery object.

JQuery.fn.data ([key [, value]]) (access to data on all elements that match the current jquery object)

JQuery.fn.removeData (keys) (removes the data item that specifies the key name stored on each element that matches the current jquery object)

Jquery.support (returns the attributes or bug information for the browser that the user is currently using.) The property is an object. The properties of this object are not immutable, and jquery does not guarantee that the specified attributes will be available in future releases, which are primarily intended for use by plug-ins or kernel developers.

Jquery.contains (container, contained) (determines whether another element is contained within the specified element.) In short, this function is used to determine whether another DOM element is a descendant of a specified DOM element.

Jquery.extend ([deep], Target, Object1 [, objectn ...]) (Merges the contents of one or more objects into the target object.) This function can copy the member properties and methods of one or more objects to the specified object, and the parameter deep is used to indicate whether depth is recursive merging.

JQuery.fn.extend (object) (extends one or more instance properties and methods for jquery (primarily for extension methods))

Jquery.globaleval (code) performs a section of JavaScript code globally, similar to the regular JavaScript eval () function. The difference is that the scope of the Jquery.globaleval () execution code is global scope)

Jquery.grep (array, function [, invert]) (filters the elements in the array using the specified function, and returns the filtered array.) The source array is unaffected and the filtered results are reflected only in the returned array of results.

Jquery.inarray (value, array [, Fromindex]) (searches the array for the specified value and returns its index value.) If the value is not present in the array, return-1)

Jquery.isarray (object) (determines whether the specified parameter is an array)

Jquery.isemptyobject (object) to determine whether the specified parameter is an empty object. The so-called "null object", that is, does not include any enumerable (custom) properties. In short, the object has no attributes that can be iterated through for...in.

Jquery.isplainobject (object) to determine whether the specified parameter is a pure object. The so-called "pure object", which is created by "{}" or "New object".

Jquery.isfunction (object) (determines whether the specified parameter is a function)

Jquery.isnumeric (value) (determines whether the specified parameter is a numeric value)

Jquery.iswindow (object) (determines whether the specified parameter is a window)

Jquery.isxmldoc (node), which determines whether one of the DOM nodes is in an XML document or is itself an XML document. This function is primarily used to determine whether a specified document is an XML document or an HTML (or XHTML) document)

Jquery.makearray (object) Converts a class array object to a true array object. The so-called "Class array object" is a regular object object, but it is very similar to the array object: It has the length attribute and is 0, 1, 2, 3 ... Number as the property name. But it's not an array, it's not a built-in method that inherits from an array's prototype object (for example, push (), sort (), etc.)

Jquery.noop () (is an empty function, it does nothing.) When you need to pass in a function argument and want it to do nothing, you can use that function, and you don't need to create a new empty function.

Jquery.now () returns the number of milliseconds that elapse between the current time from Midnight January 1, 1970. This function acts like new Date (). GetTime ())

Jquery.parsehtml (htmlstring [, Context] [, keepscripts]) (parses the HTML string into the corresponding DOM node array.) This function converts HTML strings into a collection of DOM elements using native DOM element creation functions, and you can insert these DOM elements into the document.

Jquery.parsejson (jsonstring) converts a well-formed JSON string to its corresponding JavaScript object. "Well-Formed" means that the specified string must conform to the strict JSON format, for example: The property name must be double quotes, the string value must also be in double quotes. If you pass in a JSON string that is not well-formed, a JS exception is thrown.

Jquery.parsexml (xmlstring) (resolves the string to the corresponding XML document.) The function uses the parser's built-in parsing function to create a valid XML document that can be passed in to the jquery () function to create a typical jquery object for traversal or other operations.

Jquery.trim (str) (removes white space characters at both ends of the string.) This function can remove white space characters at the beginning and end of the string until the first non-white-space string is encountered. It clears common whitespace characters, including line breaks, spaces, tabs, and so on.

Jquery.type (object) determines the type of JavaScript built-in object and returns the type name in lowercase. JavaScript also comes with a typeof operator that can determine the type of data. However, for most objects, the typeof operator returns "Object" and cannot distinguish between specific types. Jquery.type () can more precisely determine the type of JS built-in object. For example: for new number (5), TypeOf returns "Object", Jquery.type () returns "number", and for New Date (), TypeOf returns "Object" and Jquery.type () returns "Date". The returned result of type has the lowercase of "Boolean number String Function Array Date RegExp Object Error"

Jquery.unique (array), which sorts the DOM element arrays according to the order in which the elements appear in the document, and removes the duplicate elements.

  Note: This function is used only for DOM element arrays, not numbers, strings, or other types. In addition, the repetition here refers to the fact that two elements are actually the same element (by the congruent "= = ="), not the same elements as the two attributes.

Warning: The actual test found that the function did not return the correct result as expected. In different browsers, in different versions of jquery, the results may be inconsistent (refer to the demo code below)

JQuery.fn.promise ([Type,] obj) (Gets the promise of the resolved delay object and merges with the Obj object. And the specified type of queue is emptied (the default type is FX) to add resolution Processing)

A.jquery.trim Source code Detailed

The TRIM function has two branches, the first of which is: if the browser supports the TRIM function, the browser's local trim function is used; otherwise, take the second branch. Use regular to remove both front and back blanks.

If you can, use the browser-supported trim function
//Core_version.trim
JQuery.trim:core_trim &&!core_trim.call ("\ufeff\xa0") ?
function (text) {return
  text = null? "
  ":
  core_trim.call (text);
}:
//otherwise use regular removal of whitespace characters at both ends
//r Trim =/^[\s\ufeff\xa0]+| [\s\ufeff\xa0]+$/g,
function (text) {return
  text = null? "
  ":
  (text + ""). Replace (RTrim, ""); c15/>},

B. Queues (queue) detailed

The JQuery.fn.queue (type, data)
processing steps are as follows:
The default queue is the standard animation effect queue for the FX type. If the queue type is not a string, you need to adjust the data by default type.
if (typeof type!== "string") {
  data = type;
  Type = "FX";
  setter--;
}

Determines whether a queue of the specified type is fetched or set based on the parameters.

If it is a queue that obtains the corresponding type of the first element that directly obtains the current jquery matching element;

Gets the specified type of queue
if (Arguments.length < setter) {return
  jquery.queue (this[0], type);

If set, the current jquery-matched element is traversed, the specified type of queue is set for each element, and the corresponding hooks is set for each element (used for extension processing, such as the last Purge queue usage)

return data = = undefined?
This:
//Each jQuery element adds a queue
This.each (function () {
  var queue = Jquery.queue (this, type, data);
  Make sure the queue has a hooks. After this code is executed, a function of emptying the queue is saved empty
  jquery._queuehooks (this, type);
  If "FX" represents a standard animation effect queue in jquery, and the first function in the queue is not executing
  //, the first function in the queue is executed. The visible animation queue is added and immediately executes the animation
  if (type = = "FX" && Queue[0]!== "inprogress") {
    jquery.dequeue (this, type);
  }
})

The function uses the low-level API Jquery.queue function, is gets/sets the base interface of the queue, the source code is as follows

Queue:function (Elem, type, data) {
  var queue;
  if (elem) {
    //First get the corresponding type of queue
    type = (Type | | "FX") + "queue";
    Queue = Jquery._data (Elem, type);
    At the end of the queue, add the function
    if (data) {
      if (!queue | | jquery.isarray (DATA)) {
        queue = Jquery._data (Elem, type, Jquery.mak Earray (data));
      else {
        Queue.push (data);
      }
    }
    Return Queue | | [];
  }
}

C.jquery.fn.dequeue

Removes the first function in the specified queue for each matching element and performs the removed function. Internal call Jquery.dequeue to implement. Jquery.dequeue source code is as follows

Special attention in the Jquery.dequeue is the processing of the FX animation queue

Dequeue:function (Elem, type) {
  type = Type | | "FX";
  var queue = Jquery.queue (Elem, type),
    startlength = queue.length,
    fn = Queue.shift (),//Remove the first function in the queue
    hooks = JQ Uery._queuehooks (Elem, type),
    next = function () {
      jquery.dequeue (elem, type);
  If the FX queue is out, always remove the progress point
  if (fn = = "InProgress") {
    fn = Queue.shift ();
    startlength--;
  }
  Hooks.cur = fn;
  if (FN) {
    //Add a progress point to prevent the FX queue from automatically dequeue
    if (type = = "FX") {
      queue.unshift ("inprogress");
    }
    Clears the last queue stop function
    Delete hooks.stop;
    Next and hooks are passed to the callback
    Fn.call (Elem, Next, hooks)
  ;
  Queue Length is 0 and hooks exists, delete queue
  if (!startlength && hooks) {
    hooks.empty.fire ();
  }
}

Note the parameters passed by functions in the execution queue (Elem, Next, hooks).

The above is a small series for you to share the jquery 1.9.1 Source Analysis Series (14) of the common jquery tools, I hope you like.

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.