")' instead of '$ ("
")'? popup = $("
") .css(settings.popupCSS) .attr("id", settings.popup...">

Why is this jQuery link '$ ("<div /"> ")' instead of '$ (" <div> </div> ")'?

popup = $("<div />") .css(settings.popupCSS) .attr("id", settings.popupId) .css("position", "absolute") .appendTo("body").hide(); 

I am reading jQuery code and I am a bit confused which means $("<div />") . Is this just a reference to the <div /> instance that appears at that moment?

+2
source share
5 answers

Technically, it doesn't matter if you use $('<div />') or $('<div></div>') .

What this code does is create a new div element, add some CSS styles to it, add an identifier, position it, add it to the body and then hide it.

I assume jQuery uses document.createElement to create the element, which means the browser knows how to display it.

+1
source

jQuery allows you to use $("<p><em>Your</em> HTML here!</p>") to create a new HTML element that can later be inserted into the document (for example, using .append() ).

<div /> is the XML / XHTML syntax for a "self-closing" element (one that does not require a closing tag). In this case, this is equivalent to using <div></div> . <div> is not usually considered self-closing, but jQuery supports it anyway.

+5
source

This is the same as <div></div> . It doesn't matter which one you use

+2
source

A $("<element />") creates a new element of this type that is not bound to the DOM (document object model). Once they finish setting up the div, they will almost certainly add it to the document later. As other users have already said, using <div></div> or <div /> does not matter in this case, since both will create a new empty div element.

+2
source

$("<div />") creates a new div tag as a jQuery object and assigns it to the popup variable. The rest applies the id style settings and adds it to <body></body> using display: none .

If you were to write out html for the div tag, it would read: <div id="{value of settings.popupId}" style="{value of settings.popupCSS}; position:relative;"></div>

The appendTo application adds it to the body, and hiding it hides it:

 <body> <div id="{value of settings.popupId}" style="{value of settings.popupCSS}; position:relative; display:none;"></div> </body> 
+1
source

Source: https://habr.com/ru/post/899175/


All Articles