hide - Understanding jQuery
hide - Understanding jQuery
hide - Understanding jQuery
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
he asterisk selector (*) is the default behavior of any CSS selector whether it is specified or not.<br />
In other words, an identical solution could look something like this:<br />
$(“* body :not(p)”).<strong>hide</strong>();<br />
Again, being explicit in telling <strong>jQuery</strong> exactly what we want to select fixed the problem. You<br />
can see the true hierarchy forming. First the asterisk, then body, then everything else.<br />
So, even though only :not(p) is the true defining point of what is being selected, you can see<br />
how using the understanding of the DOM hierarchy itself also plays a big role in selecting elements.<br />
If you find that your selector isn’t working for some reason, check its hierarchy integrity.<br />
The best bet is to always be as specific as possible.<br />
Looks like we’ve come around a full circle. We started this lesson with saying that the selectors<br />
$(“* :not(p)”) and $(“:not(p)”) do not produce a result that at first they seem they should.<br />
We then learned that in order to solve the problem we should explicitly indicate the context<br />
whether it was the asterisk selector, the body tag, or both.<br />
It should be easy to see now that $(“* :not(p)”) and $(“:not(p)”) are one and the same thing<br />
exactly. There is absolutely no difference between the two selectors and the second one assumes<br />
the asterisk by default anyway.<br />
A Quick Note About Explicit Default CSS Definitions<br />
While doing research about the inner workings of CSS selectors in the context of <strong>jQuery</strong> I<br />
noticed something rather interesting. Even a selector such as $(“* :not(p)”).<strong>hide</strong>(); that we<br />
just discussed above would not work quite the same under certain conditions. What are these<br />
conditions?<br />
First we must understand that the asterisk is not the same as body. The asterisk assumes absolutely<br />
everything. Even the tag. And that’s the key to understanding why some CSS<br />
selector may seem like they do not work in some instances. Remember that the tag<br />
itself can contain CSS styles. If it does, it will certainly affect the tag as well.<br />
To fix this problem we should stick to one simple rule. If you ever wish to use the asterisk<br />
selector, you must always specify the default CSS style for everything using the asterisk command<br />
within the default style.css file. As in: * { background: white; } Doing this will reset the<br />
global style for every single tag on the page. If you don’t do this you will find that the asterisk<br />
selector may not behave as intended. So the two core principles working with * and body selectors<br />
are to always provide a default CSS style and be explicit with your CSS selectors.<br />
106