domManip
connection, here’s all the code for the append function: append: function() { return this.domManip(arguments, true, function( elem ) { if ( this.nodeType === 1 || this.nodeType === 11 ) { this.appendChild( elem ); } } ); },
ELEMENT_NODE
or DOCUMENT_FRAGMENT_NODE
) of our set in the current jQuery object will be added through an ordinary appendChild
element from the parameters to the function.clean
all scripts are additionally collected that will not fall into the resulting fragment, but return to a separate array.domManip
(respectively, and the functions that use it) are able to receive a function at its input, in which case it will be called for each element in a jQuery object in its context to get what we want to pass to it. <span class="user" data-id="15"></span> <span class="user" data-id="10"></span> <script src="http://code.jquery.com/jquery-1.8.3.js"></script> <script> // - , $('span.user').prepend( function(idx, html) { // , // console.log(idx, html); // $.data return $(this).data('id') + ': '; } ); </script>
span
with the “user” class, the contents of which will contain the user ID: jQuery.fn.prepend = function() { // span' jQuery- return this.domManip(arguments, true, function( elem ) { // // span.user, (this) // 1 -> ELEMENT_NODE // 11 -> DOCUMENT_FRAGMENT_NODE // span' if ( this.nodeType === 1 || this.nodeType === 11 ) { // elem ( - "xx: ") // span ( , ) this.insertBefore( elem, this.firstChild ); } } ); };
script
with the src
attribute, then it will be pre- synchronous (to follow the order of execution of scripts) loaded. These scripts won't get into the DOM. $('<div>').append('<script>alert(1);</script>')
div
hangs in the air, it is not in the document, but the script will still be executed in this case.src
. The same with jQuery.getScript and jQuery.ajax with the type of script
(all this is analog). By default, jQuery thinks that this does not need to be cached and adds a parameter with the current unix-timestamp to the URL when loading. In this case, if the browser wants to cache the response from the server (it all depends on the headers), then it will cache it according to the url, where there will be a timestamp, which in some (most likely even in most) cases is not acceptable, because the next similar request will again go past the cache and will again be cached with a timestamp in the url. $('<script>', { 'src': 'http://code.jquery.com/jquery-1.8.3.min.js' } ).appendTo(document.body);
var scriptElement = document.createElement('script'); scriptElement.setAttribute('src', 'http://code.jquery.com/jquery-1.8.3.min.js'); document.body.appendChild(scriptElement);
domManip
and deal with removing elements from the tree through the usual removeChild . jQuery.empty
removes all the nested elements of the victim, and jQuery.remove
removes only those nodes that match the selector specified in the parameter to the call of this function. Both methods call the unknown function cleanData , which we will touch on in the next part.innerHTML
property, from which the jQuery service attributes will be previously removed.script
, style
, link
tags and the first tag is not found in wrapMap
we are familiar with in the previous article) will try to set the innerHTML
property directly , otherwise, first clear the contents of our tag with empty
, and then add code to it with append
. $('<span>').appendTo(document.body) => $(document.body).append('<span>') $('span.user').insertAfter('span:first') => $('span:first').after('span.user')
Source: https://habr.com/ru/post/164677/
All Articles