It's a new way of working with javascript, and it's incredibly concise.
|
|
Let's see what we can do with single lines of jQuery: Click here to make new friends for( i=0; i<35; i++ ){ $("#box").append('<div class="friend"></div>'); } That was easy, but they look bored. Let's make them dance. setInterval( function(){ $(".friend").each( function(){ $(this).fadeTo('fast',Math.random()); } ); }, 500 ); You call that dancing? setInterval( function(){ $(".friend").each( function(){ $(this).fadeTo('fast',Math.random()); } ); }, 250 ); They're dancing too hard! Stop! setInterval( function(){ $(".friend").each( function(){ if( Math.random() > 0.9 ){ this.zIndex = 666; this.style.background = 'url(dead.gif) center center no-repeat'; $(this).fadeTo('slow',0.2 ) } } ) }, 250 ); |
If you want an in depth lesson on jquery, start here.
Otherwise, all you need to know is that it takes code like this:
Traditional Javascript:
var element = document.getElementById("box");
if( element.style.display != 'hidden' ){
element.style.display = 'hidden';
} else {
element.style.display = 'block';
}
And replaces it with something like this:
JQuery:
$('#box').toggle();
JQuery is also useful for more practical tasks. Want a slideshow, voting system, or form autocompletion? JQuery makes it all easy, whether you code from scratch or use any of the hundreds of free, open source plugins.
It's not just pretty, either. Dynamic loading with jQuery's ajax functionality can lower server load dramatically.
Let's look at a simple ajax request to pull some content from the server:
JQuery code:
$.get("content.xml","",function(x){ $("target").html( x ) });
I'd include the alternative code here, but it's so long that it would simply be rude to do so.
That's the basics of jQuery. Really simple interactive coding. Plus, it's free and open source.
Read more about it at jquery.com, or check out our eValuator, which uses jQuery for visual effects.
Recent comments
42 weeks 5 days ago
1 year 11 weeks ago
1 year 27 weeks ago
1 year 39 weeks ago
1 year 47 weeks ago
2 years 13 weeks ago
2 years 36 weeks ago
2 years 46 weeks ago