JQuery指南/基础

 

 这是一个基础指南,旨在帮助你开始使用jquery。jquery给予你常见问题的解决方法。如果你尚未建立你的测试页面,我建议你创建一个含有下列内容的HTML页:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" οnclick="if(!this.resized) {return true;} else {window.open(this.src);}" alt="" src="http://docs.google.com/File?id=dc2893qz_289vxzb" οnlοad="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" border=0>程序代码
<html>
<head>
<script type="text/javascript"
src="link/to/jquery.js"></script>
<script type="text/javascript">
   // Your code goes here
</script>
</head>
<body>
<a href="http://jquery.com/">jQuery</a>
</body>
</html>


      修改script标签的src属性指向到你的jquery.js。例如,如果你的jQuery.js与你的HTML文件在同一目录,你可以这样:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" οnclick="if(!this.resized) {return true;} else {window.open(this.src);}" alt="" src="http://docs.google.com/File?id=dc2893qz_289vxzb" οnlοad="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" border=0>程序代码
<script type="text/javascript" src="jquery.js"></script>
文档载入时运行代码
     首先, 大多数JavaScript程序员会用类似代码:  
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" οnclick="if(!this.resized) {return true;} else {window.open(this.src);}" alt="" src="http://docs.google.com/File?id=dc2893qz_289vxzb" οnlοad="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" border=0>程序代码
window.onload = function(){ ... } .

     访问HTML文档的元素,必须先载入文档对象模型(DOM)。当window.onload函数执行的时候,说明所有东西已经载入,包括图像和横幅等等。要知道较大的图片下载速度会比较慢,因此用户必须等待大图片下载完毕才能看到window.onload()执行的代码效果,这样就花费了很长的等待时间,这不是我们想要的。

        对于此,jquery提供了一个"ready"事件,你可以使用以下的代码片段:  
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" οnclick="if(!this.resized) {return true;} else {window.open(this.src);}" alt="" src="http://docs.google.com/File?id=dc2893qz_289vxzb" οnlοad="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" border=0>程序代码
$(document).ready(function(){
// 你的代码
});

       $(document)意思是说,获取文档对象(类似的于window.document),$(document).ready意思就是说,获取文档对象就绪的时候。
       上面这段代码的意思是检查文档对象直到它能够允许被操作(译者注:这样做比window.onload()函数要快的多,因为只要文档对象载入完成就能够执行代码了,而不需要等待页面中的图片下载是否已经完成)---这是我们想要的。因此将上面的代码片段粘贴到你测试页面的脚本区吧!

鼠标点击时的触发
      
首先,我们尝试鼠标点击超链接时触发某些行为。在ready函数里加入以下代码:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" οnclick="if(!this.resized) {return true;} else {window.open(this.src);}" alt="" src="http://docs.google.com/File?id=dc2893qz_289vxzb" οnlοad="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" border=0>程序代码
$("a").click(function(){
alert("谢谢你的来临!");
});

       保存HTML文件,然后刷新一下页面。点击某个超链接,页面将弹出警告对话框。

增加 CSS Class

       另外一个事情就是,一个共同的任务:增加或移除元素的css class,例如:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" οnclick="if(!this.resized) {return true;} else {window.open(this.src);}" alt="" src="http://docs.google.com/File?id=dc2893qz_289vxzb" οnlοad="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" border=0>程序代码
$("a").addClass("test");
$("a").removeClass("test");

    如果你已经在页面头部加入了:  
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" οnclick="if(!this.resized) {return true;} else {window.open(this.src);}" alt="" src="http://docs.google.com/File?id=dc2893qz_289vxzb" οnlοad="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" border=0>程序代码
<style>a{text-weight:bolder}</style>
    那么当你调用了addClass函数后,所有超链接的字体将变成粗体。

特效
   
Effects Module(效果模块)提供了一系列好用的特效。

    加上下面代码:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" οnclick="if(!this.resized) {return true;} else {window.open(this.src);}" alt="" src="http://docs.google.com/File?id=dc2893qz_289vxzb" οnlοad="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" border=0>程序代码
$("a").click(function(){
    $(this).hide("slow");
    return false;
});

    现在,只要你点击超链接,超链接就会慢慢的消失。“return false"表示保留默认行为,因此页面不会跳转。

回调

   
所谓回调就是父函数执行完成后,自身能够作为返回值传递到另一个函数的函数。回调功能的特别之处在于,出现在“父函数"后面的函数可以在回调函数执行前执行。
    另外一个重点是要知道如何正确运用回调,我就常常忘记了正确语法。

    一个不带参数的回调应该这样写:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" οnclick="if(!this.resized) {return true;} else {window.open(this.src);}" alt="" src="http://docs.google.com/File?id=dc2893qz_289vxzb" οnlοad="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" border=0>程序代码
$.get('myhtmlpage.html', myCallBack);
    注意第二个参数是一个简单的函数名(它不是字符,也没有带括号)

    那么带参数的回调该怎么写呢?
   
错误的写法,下面这样写是不行的(或者不会执行):
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" οnclick="if(!this.resized) {return true;} else {window.open(this.src);}" alt="" src="http://docs.google.com/File?id=dc2893qz_289vxzb" οnlοad="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" border=0>程序代码
$.get('myhtmlpage.html', myCallBack(param1,param2));
    正确的写法:
screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" οnclick="if(!this.resized) {return true;} else {window.open(this.src);}" alt="" src="http://docs.google.com/File?id=dc2893qz_289vxzb" οnlοad="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" border=0>程序代码
$.get('myhtmlpage.html', function(){
    myCallBack(param1,param2);
});

    这样就实现了回调一个带参函数的功能。

后记
   
   到这里,也许你应该去看看其余的 文档了。里面包括更多的 指南-它很全面,涵盖了jquery各个方面。如果大家有问题,请放心的给我发Email。
    当然,你也可以看看利用jQuery做的多种 DEMO
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值