$(this) keyword on jQuery -


is trying to understand how this keyword works for example this code But, is it referring to "button"? (Or click on button to take action) ("click", function () {var exemption = $ (this) .closest (click on) ($ ("button"). ".tour") data ( "Discount"); var message = $ (1-555-jquery-air. & Lt; / span & gt; "$ 1 for discount $ 1;); $ (this) closest (" Tour "). Attachment (message); $ (this) .remove ();});});

HTML is here:

  & lt; Div id = "tour" & gt; & Lt; H1 & gt; Guided tours & lt; / H1> & Lt; Ul & gt; & Lt; Li class = "USA tour" data-discount = "2 9" & gt; & Lt; H2 & gt; New York, New York & lt; / H2 & gt; & Lt; Span class = "details" & gt; $ 1899 for 7 nights & lt; / Span & gt; & Lt; Button class = "book" & gt; Book now & lt; / Button & gt; & Lt; / Li & gt; & Lt; Li class = "Europe tour" data-discount = "176" & gt; & Lt; H2 & gt; Paris, France & lt; / H2 & gt; & Lt; Span class = "details" & gt; $ 2,299 for 7 nights & lt; / Span & gt; & Lt; Button class = "book" & gt; Book now & lt; / Button & gt; & Lt; / Li & gt; & Lt; Li class = "Asia tour" data-discount = "34 9" & gt; & Lt; H2 & gt; Tokyo, Japan & lt; / H2 & gt; & Lt; Duration class = "description" & gt; $ 3,799 for $ 779 & lt; / Span & gt; & Lt; Button class = "book" & gt; Book now & lt; / Button & gt; & Lt; / Li & gt; & Lt; / Ul & gt; & Lt; / Div & gt;    

In that code, this is a reference element which Was clicked (actual DOM button element). $ (this) is an function call , in this in $ () and a jQuery object is returned . It's a wrap around the jQuery object DOM element.

Such code is repeatedly saying with the same logic as $ () . It is not particularly harmful, in this context, but if you were in a tight loop or something, then you want to call it once and keep it in context.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -