Tutorial Jquery 007
Tutorial Jquery 007
This is a basic tutorial, designed to help you get started using jQuery. If you don't have a test page setup yet, start by creating a new HTML page with the following contents: <!doctype html> <html> <head> <meta charset="utf-8"> <title>Demo</title> </head> <body> <a href="http://jquery.com/">jQuery</a> <script src="jquery.js"></script> <script> </script> </body> </html> Edit the src attribute in the script tag to point to your copy of jquery.js. For example, if jquery.js is in the same directory as your HTML file, you can use: <script src="jquery.js"></script> You can download your own copy of jQuery from the [edit]
// Your code here }); Inside the ready event, add a click handler to the link: $(document).ready(function(){
$("a").click(function(event){ alert("Thanks for visiting!"); });
}); Save your HTML file and reload the test page in your browser. Clicking the link on the page should make a browser's alert pop-up, before leaving to go to the main jQuery page. For click and most other events, you can prevent the default behaviour - here, following the link to jquery.com - by calling event.preventDefault() in the event handler: $(document).ready(function(){ $("a").click(function(event){ alert("As you can see, the link no longer took you to jquery.com");
event.preventDefault();
Complete Example
The following is an example of what the complete HTML file might look like if you were to use the script in your own file. Note that it links to Google's CDN to load the jQuery core file. Also, while the custom script is included in the <head>, it is generally preferable to place it in a separate file and refer that file with the script element's src attribute <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery demo</title> </head> <body> <a href="http://jquery.com/">jQuery</a> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> <script> $(document).ready(function(){ $("a").click(function(event){ alert("As you can see, the link no longer took you to jquery.com");
$("a").addClass("test"); All your a elements will now be bold. To remove the class, use
removeClass
Special Effects
In jQuery, a couple of handy effects are provided, to really make your web site stand out. To put this to the test, change the click that you added earlier to this: $("a").click(function(event){ event.preventDefault(); $(this).hide("slow"); }); Now, if you click any link, it should make itself slowly disappear.
[edit]
Wrong
The Wrong Way (will not work!) $.get('myhtmlpage.html', myCallBack(param1, param2));
This will not work because it calls myCallBack(param1, param2) and then passes the return value as the second parameter to [edit]
$.get()
Right
The problem with the above example is that myCallBack(param1, param2) is evaluated before being passed as a function. Javascript and by extension jQuery expects a function pointer in cases like these. I.E. setTimeout function. In the below usage, an anonymous function is created (just a block of statements) and is registered as the callback function. Note the use of 'function(){'. The anonymous function does exactly one thing: calls myCallBack, with the values of param1 and param2 in the outer scope. $.get('myhtmlpage.html', function(){ myCallBack(param1, param2); }); param1 and param2 are evaluated as a callback when the '$.get' is done getting the page.
[edit]
MORE READING
From here, you should probably begin looking at the rest of the Documentation - it's very comprehensive and covers all aspects of jQuery. If you have any questions, please feel free to send a message to the jQuery Forums