我们学习所有的编程语言都是从helloworld开始,jquery的第一个程序也从这里开始了!
不过在此之前要去jquery的官网下载jquery的库,网址如下:http://jquery.com/,最新版本为1.7.2,本系列的测试版本均为1.7.2
helloworld程序如下:
$(document).ready(function() { // ready的作用是当页面中dom加载完毕后执行参数中的函数
alert("Hello,world!");
}); // outputs Hello,world!
ready和onload有什么区别呢?
下面我们看一段onload的代码:
function test1() {
alert("hello");
}
function test2() {
alert("world");
}
window.onload = test1;
window.onload = test2; // outputs world
// 如果是ready会是怎样呢?
$(document).ready(function() {
alert("hello");
});
$(document).ready(function() {
alert("world");
}); // 先输出hello,后输出world