jQuery 开天辟地入门篇一

2020-05-22 16:31:51易采站长站整理

5.太多了! 等待我们一一去发现. 四.Hello World jQuery
按照惯例, 我们来编写jQuery的Hello World程序, 来迈出使用jQuery的第一步.
在本文最后可以下本章的完整源代码.
1.下载jQuery类库

jQuery的项目下载放在了Google Code上, 下载地址:


http://code.google.com/p/jqueryjs/downloads/list


上面的地址是总下载列表, 里面有很多版本和类型的jQuery库, 主要分为如下几类:


min: 压缩后的jQuery类库,  在正式环境上使用.如:jquery-1.3.2.min.js


vsdoc: 在Visual Studio中需要引入此版本的jquery类库才能启用智能感知.如:jquery-1.3.2-vsdoc2.js


release包: 里面有没有压缩的jquery代码, 以及文档和示例程序. 如:jquery-1.3.2-release.zip

2.编写程序创建一个HTML页面, 引入jQuery类库并且编写如下代码:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>Hello World jQuery!</title>
<script type=”text/javascript” src=”scripts/jquery-1.3.2-vsdoc2.js”></script>
</head>
<body>
<div id=”divMsg”>Hello World!</div>
<input id=”btnShow” type=”button” value=”显示” />
<input id=”btnHide” type=”button” value=”隐藏” /><br />
<input id=”btnChange” type=”button” value=”修改内容为 Hello World, too!” />
<script type=”text/javascript” >
$(“#btnShow”).bind(“click”, function(event) { $(“#divMsg”).show(); });
$(“#btnHide”).bind(“click”, function(event) { $(“#divMsg”).hide(); });
$(“#btnChange”).bind(“click”, function(event) { $(“#divMsg”).html(“Hello World, too!”); });
</script>
</body>
</html>


效果如下:


image 


页面上有三个按钮, 分别用来控制Hello World的显示,隐藏和修改其内容.


此示例使用了:


(1) jQuery的Id选择器: $(“#btnShow”)


(2) 事件绑定函数 bind()


(3) 显示和隐藏函数. show()和hide()


(4) 修改元素内部html的函数html()


在接下来的教程中我们将深入这些内容的学习.


 


五.启用Visual Studio 对jQuery的智能感知

首先看一下Visual Studio带给我们的智能感知惊喜. 要让Visual Studio支持智能感知, 需要下列条件: