7 ms·
Things You Might Not Know about jQuery
- NathanKP 16y agoThe only thing on this list that I didn't already know was that you could bind "ajaxStart" and "ajaxStop". This will prove very useful in the future.
- mhb 16y agoLink to the book: http://www.amazon.com/jQuery-Pocket-Reference-David-Flanagan/dp/1449397220/ http://www.amazon.com/jQuery-Pocket-Reference-David-Flanagan...
- niyazpk 16y agoThe first example for the 2nd point is wrong: The following two lines have the same effect, for example: $('pre').attr("css", {backgroundColor:"gray"}) $('pre').css({backgroundColor:"gray"}) Not correct. The first line will create an attribute called 'css' while the second line will change the 'style' attribute. The author probably meant something like: $('pre').attr("style", {backgroundColor:"gray"}) $('pre').css({backgroundColor:"gray"}) ...but even these two lines are not exactly the same. The first one will reset the style attribute to the given value while the second line will just change the backgroundColor value in the style attribute.
- adolph 16y agoThe second example doesn't work for me. var image = $("").appendTo("body"); //Nothing appended to body. I think it should be: var image = $("<img>").appendTo("body"); //img node appended to body.
- Sidnicious 16y agoIt looks like the `<img/>` in between the quotes was not escaped; it's in the source.
- stanleydrew 16y agoHe is claiming that the value "css" is special, along with a few others, in that when you call attr with it jQuery actually dispatches to the css method passing the second argument as the value. He claims in the comments that this works with 1.4 (http://answers.oreilly.com/topic/2353-5-things-you-might-not-know-about-jquery#post_id_4373 http://answers.oreilly.com/topic/2353-5-things-you-might-not...), but I can't find it documented anywhere in jQuery's docs, nor can I make it work with the version of jQuery served on the jQuery docs site itself.
- pak 16y agoIIRC from the last time I browsed through jQuery source, attr('css', ...) is used internally but didn't look like it was meant to be exposed as a public method. And I never remember seeing this sort of special case in the docs. So, I wouldn't recommend using it in kosher jQuery code. A lot of things in this article seem a little dated; almost every tutorial now covers the $(function() {}) shortcut, and event binding is something I wouldn't know about jQuery? Really. Note to jeresig: this article illustrates a beef I remember passing on when the jQuery docs reorganized; it works well as API reference now, but before, introductory text for each section of the docs describing things like event namespacing made for a nicer introduction to beginners like me, as well as the structured sub-organization of methods within each section. Now, it's more of an alphabetical method soup, so a lot of beginners probably never bother to learn that event namespaces exist, or what the jQuery event object does. For example, you can only get to the documentation on the event object from within the documentation for methods like .bind(), which I find odd. (How would a beginner know that it even existed unless they went specifically to the .bind() docs in an alphabetical list of other cooler sounding methods?)
- mildweed 16y agocould somebody please post an example to point #1?
- niyazpk 16y agoInstead of: $(document).ready(handler); you can write: $(handler);
- kilian 16y agocommonly used like this: $(function() { // write the stuff you normally put in $(document).ready( ... ); });
- niyazpk 16y agoJust in case anyone finds it useful, use this when you have to mix jQuery with other JavaScript libraries like prototype: jQuery(function($) { // your code using $ }(jQuery)); This way you can use $ to reference jQuery in your code even if the other library took control of $ in the global scope.
- misterbwong 16y agoWe went with jQuery in no conflict mode when we needed to mix jquery and the prototype lib for ads. from the docs: var j = jQuery.noConflict(); // Do something with jQuery j("div p").hide(); // Do something with another library's $() $("content").style.display = 'none';
- davecardwell 16y agoYou don’t need that jQuery parameter (in fact, I think that makes it invalid JavaScript). ie. jQuery(function($) { // your code using $ }); You might be thinking of the recommended method of creating a closure, in which the code is executed immediately rather than upon page load: (function($) { // your code using $ })(jQuery);
- csomar 16y agoThe first sentence really stops me from reading it jQuery is a very powerful library, but some of its powerful features are obscure, and unless you've ready the jQuery source code, or my new book jQuery Pocket Reference, you may not know about them. Why don't you give me the article first, let me read it, get amazed by the techniques and then find out about you and your book and feel like I need to read another book. This way I feel like you are trying to sell me your stuff.
- mhb 16y agoExcept that there's no link to the book which I suppose one could consider more or less intrusive.
- sp4rki 16y agoPerhaps because you are reading an article in O'Reilly's website, whom sells David Flanagan. That out of the way, I thinkn he's entitled to a little self promotion at any point in the article since this is the man behind such gems as JavaScript: The Definitive Guide, and The Ruby Programing Language. He is trying to sell you stuff, but honestly you should go right ahead and buy what he's advertising because his books are really that good.
- JonnieCache 16y agoSeconded. Advertising for outstandingly good products is not a bad thing.
- deleted 16y ago[deleted]
- sp4rki 16y agoYeah I noticed it too late to edit the post. I'm on the 30th hour of a coding marathon and I'm starting to feel my neurons burning up and leaving me numb as hell. You can easily spot places where my train of thought kind of crashed into a wall. Oh well, c'est la vie.
- 16y ago
- Xurinos 16y agoYou don't have to use $(document).ready() anymore As far as I know, this was always the case. The reason people use $(document).ready() is that it feels more correct, is more consistent. jQuery() supports three distinct behaviors: $(function) => jQuery(document).ready(function) $(objects) => Wrap objects with jQuery functionality $("selector") => Find objects, and wrap them with jQuery functionality One of these is not like the others.
- deleted 16y ago[deleted]