Need to learn JavaScript? jQuery is a JavaScript framework, so if you don't already know about the JavaScript programming language, we recommend that you learn it now: Learn JavaScript

This article is currently in the process of being translated into Chinese (~1% done).


Hello, world!


<div id="divTest1"></div>
<script type="text/javascript">
$("#divTest1").text("Hello, world!");

好的,首先我们有一个id为"divTest1"的div标签。然后在Javascript代码中,我们使用$快捷访问jQuery,去选择"divTest1"底下所有的元素(虽然这里只有一个),并把它们的文本设置为"Hello, world!"。你或许还没有足够关于jQuery的知识去理解这个例子的工作原理与方式,但是随着你继续学习本教程,所有元素都将得到详细的解释。

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!";

And it would be even longer if our HTML element didn't have an ID, but for instance just a class.

Normally though, you should 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. Fortunately, jQuery makes this very easy as well, as we will see in the next chapter. After that, we will start looking into one of the most important aspects of jQuery, which has already been used in the above example: Selectors.

This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!