1
How this function jQuery finds the tag em
to execute the method prependum() ?
var em;
$("#btn").click(function() {
if (em) {
em.prependTo("pre");
em = null;
} else {
em = $("em").detach();
}
});
em {
background: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<em>Alô </em>
<pre>como vai</pre>
<em>você?</em>
<button type="button" id="btn">Anexa/desanexa parágrafos</button>
</div>
It does not. The first condition will be false (because
false
,null
andundefined
are considered false in Javascript), this will cause the code to jump to theelse
. Thedetach
will crop the widget<em>
and assign to the variableem
, this way it will no longer be false and will become true. The next time the button suffers a click action, the condition will be true.– Valdeir Psr
All right, man, I get it. Thanks for the clarification!
– LeAndrade