Sabtu, 10 Mei 2014

jQuery : Hello, world!

Every decent programming tutorial will start with a "Hello, world!" example and this tutorial is yet another one of them. In the previous chapter, we learned how to include jQuery on our page, so that we may start using all of its great features. You need to know a bit more about how jQuery works, before you can start writing your own code, but just to make sure that everything is working, and for you to see how simple jQuery is, let's kick off with a little example:

<div id="divTest1"></div><script type="text/javascript">
$
("#divTest1").text("Hello, world!");</script>
Okay, so we have a div tag with the id of "divTest1". In the JavaScript code we use the $ shortcut to access jQuery, then we select all elements with an id of "divTest1" (there is just one though) and set its text to "Hello, world!". You may not know enough about jQuery to understand why and how this works, but as you progress through this tutorial, all of the elements will be explained in detail.

Even such a simple task as this one would actually require quite a few extra keystrokes if you had to do it in plain JavaScript, with no help from jQuery:
<div id="divTest2"></div><script type="text/javascript">
document
.getElementById("divTest2").innerHTML = "Hello, world!";</script>
And it would be even longer if our HTML element didn't have an ID, but for instance just a class. 

Normally though, you wait for the document to enter the READY state before you start manipulating its content. The above examples will work in most browsers and likely even work when you do more advanced stuff, but certain tasks may fail if you try to do them before the document is loaded and ready. 

Source:http://www.tutorialsdesk.com/2012/08/jquery-hello-world.html

Tidak ada komentar:

Posting Komentar