Dommanip Analysis of JQuery 3.0

Source: Internet
Author: User
Tags script tag

Dommanip This function has a long history, and has existed since the jquery 1.0 version, until the latest version of Jquery. is an elder tool Function.

The main function of Dommanip is to implement the insertion and substitution of the DOM. Total of the following 5 function services

    • Internal Post-insertion (append)
    • Internal front insertion (prepend)
    • External front insertion (before)
    • External Post-insertion (after)
    • Replace element (replacewith,1.9.x Previous version does not use Dommainp)

And one each generates another 5 functions: appendTo, prependto, insertbefore, insertafter, replaceall

Jquery.each ({appendTo: "append", prependto: "prepend", insertbefore: "before", insertafter: "after", replaceall: " ReplaceWith "}, function (name, original) {jquery.fn[name] = function (selector) {var elems,ret = [],insert = JQuery (s elector), last = Insert.length-1,i = 0;for (; i <= last; i++) {elems = i = = = last? This:this.clone (true); jquer Y (insert[i]) [original] (elems);//support:android <=4.0 only, phantomjs 1 only//. get () because push.apply (_, a Rraylike) throws on ancient webkitpush.apply (ret, elems.get ());} return This.pushstack (ret);};} );

Internal call

Source

Append:function () {return dommanip (this, arguments, function (elem) {if (this.nodetype = = = 1 | | this.nodetype = = 11 | | This.nodetype = = = 9) {var target = manipulationtarget (this, elem); target.appendchild (elem);}}  );},prepend:function () {return dommanip (this, arguments, function (elem) {if (this.nodetype = = 1 | | this.nodetype = = = 11 | | This.nodetype = = = 9) {var target = manipulationtarget (this, elem); target.insertbefore (elem, target.firstchild);}} );},before:function () {return dommanip (this, arguments, function (elem) {if (this.parentnode) {this.parentNode.insert Before (elem, this);}} );},after:function () {return dommanip (this, arguments, function (elem) {if (this.parentnode) {this.parentNode.insertB Efore (elem, this.nextsibling);}}  );},replacewith:function () {var ignored = [];//make the changes, replacing each non-ignored context element with the new Contentreturn dommanip (this, arguments, function (elem) {var parent = this.parentnode;if (jquery.inaRray (this, ignored) < 0) {jquery.cleandata (getAll (this)), if (parent) {parent.replacechild (elem, this);}} Force callback invocation}, ignored);}

  

The realization of Dommanip

The main function of Dommanip is to add DOM elements, because they add different locations and provide four public functions append, prepend, before, after, plus a replacewith. In short, Dommanip did two things.

    1. Complete the DOM node additions first
    2. If the added DOM node has a script tag, additional processing is Required. For executable scripts (judged by the type attribute), the script code is executed, and the others are not executed.

An important function of Dommanip dependency is buildfragment, which improves performance for DOM Insertion.

Special handling of script node elements in Dommanip

    1. Script does not have a Type property and the JS script is executed by default
    2. Script's type= "text/javascript" or type= "text/ecmascript" will execute its JS script
    3. If a script has the SRC attribute, it executes the $._EVALURL request remote JS file and executes
    4. Other do not execute JS script, Sometimes we will use script to do HTML template, such as underscore.js,type= "text/template" or type= "text/plain" this, the inside of JS will not be executed

In addition datapriv.access (node, "globaleval"), This sentence indicates that if the script has been executed, it will not be executed Again. or, after execution, a globaleval:true is Set.


Dommanip internal dependence buildfragment, restorescript, disablescript, jquery._evalurl, domeval These small functions, and restorescript, jQuery._ Evalurl is also used only in Dommanip.

Replace/restore the type attribute of script elements for safe DOM manipulationfunction disablescript (elem) {elem.typ E = (elem.getattribute ("type")!== Null) + "/" + elem.type;return elem;} function Restorescript (elem) {var match = rscripttypemasked.exec (elem.type), if (match) {elem.type = match[1];} els E {elem.removeattribute ("type");} Return elem;} Jquery._evalurl = function (url) {return jquery.ajax ({url:url,//make This explicit, since user can override this Throu GH ajaxsetup (#11264) type: "GET", dataType: "script", cache:true,async:false,global:false, "throws": true});

  

Dommanip has undergone various versions of the evolution
    1. The Dommanip function of the previous version of 3.0.x was hung on top of the JQuery object (jQuery.fn.domManip), that is, by $ (). dommanip Way can be accessed; after 3.0.x Dommanip is a private function that cannot be accessed externally
    2. 1.2.x before the Dommanip has 4 parameters, 1.3.x ~ 1.9.x is 3 parameters; 2.x has only 2 parameters; 3.x has 4 parameters
    3. Version ReplaceWith prior to 1.9.x did not use DOMMAINP

Related:

Http://www.cnblogs.com/snandy/p/5760742.html

Dommanip Analysis of JQuery 3.0

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.