Generally speaking and simply put, it is not logical (natural) to query any structure, if the result will contain the whole structure being queried. In (for example) SQL you can not write “select everything from everywhere” or even “select ‘only this thing’ from everywhere”. In JQ we are actually almost always saying : “select this and that, from everywhere”. “everywhere” being document context. Which is not logical and not faster than having a narrower context.
I suggest for the jQuery selection context to be imposed on jQuery users. For speed of selection ? Yes this is one side of my idea. Speed.The other side is “Behaviour” or “the logic of the solution”. The logic of jQuery operating on the DOM tree.
For example and to help us understand this new behaviour. Let us imagine we make an jQuery custom selector which will work but it will be logical to use only in specific contexts ? We implement this as “~” prefix. That “~” prefix means : “a prefix before the name of the css property”. Also imagine jQuery is changed so that context MUST be given IF “~” is used. This custom css property selection will work like this:
1 2 3 4 5 6 7 8 9 |
<span style="color: #008000;">// attempt to select everything that has 'style.top' present</span> $(<span style="color: #006080;">"[~top]"</span>) <span style="color: #008000;">// selects nothing because there is no context, default will not do </span> <span style="color: #008000;">// then look into the id=container and give document.body as a context </span> $(<span style="color: #006080;">"#container[~top]"</span>, document.body ) <span style="color: #008000;">// still select nothing in the body context which is too wide </span> <span style="color: #008000;">// use '#container' as context </span> $(<span style="color: #006080;">"[~top]"</span>, <span style="color: #006080;">"#container"</span>) <span style="color: #008000;">// selects all elements with style.top, in the given context </span> |
This approach would make the whole idea of selecting the “~top” CSS properties more logical. And faster if that matters to anyone? I propose to make the whole jQuery selection, NOT to result in anything IF context which must be bellow document.body, is NOT given. Simply: No context no result.
This will give much more logical behaviour. And I suppose faster jQuery .
For attributes this is even more useful. Because attributes are much less present on the DOM tree nodes than style.properties which are almost everywhere. Browser runtime must discard a lot of nodes before finding the ones which have attributes with a required name. Narrowing a selection context in this scenario should be even more useful. And the imagined usage is this :
1 2 3 4 5 6 7 8 9 |
<span style="color: #008000;">// attempt to select everything that has attr 'data' present </span> $(<span style="color: #006080;">"[data]"</span>) <span style="color: #008000;">// selects nothing because there is no context, default will not do </span> <span style="color: #008000;">// ok look into the id=container and give document.body as a context</span> $(<span style="color: #006080;">"#container[data]"</span>, document.body ) <span style="color: #008000;">// still select nothing in the body context which is too wide </span> <span style="color: #008000;">// use '#container' as context </span> $(<span style="color: #006080;">"[data]"</span>, <span style="color: #006080;">"#container"</span>) <span style="color: #008000;">// selects all elements with attr 'data', in the given context </span> |
3 thoughts on “jQuery selector context considered a “good thing””
So if I’m getting it right, you’d like to enforce some stricter coding
standards to encourage others to always use the context argument to make the
queries faster?
That does make sense and with many elements and css querying it may
significantly speed things up, but that should be a separate plugin as it’s
independent from the purpose of attribute selectors. An interesting idea –
if that’s what you were talking about :)
2009/3/18 Balázs Endrész
Also this little preprocessor will make using your plugin ( http://plugins.jquery.com/project/EnhancedAttributeSelectors ) faster.
And if one is too “lazy” to allways think of contexts for her selectors, then the one will simply not include this pre-processor.
jQ documentation
indeed says that context is either: element or jQuery.
Here, I think it is silly to give jQuery instance as a context.
Because it obviously does not achieve any speed advances,
So the last might be the best :
Enforce context usage and enforce the context to be dom element.
This idea can be now enforced like this :
Of course (c) goes for initial excellent snippet to : Balázs Endrész