JQuery Study Notes 学习笔记(一)


1. 使用jquery
  到jquery.com下载jquery.js当前版本是1.4.2
  新建一个html页面
复制代码 代码如下:

<!DOCTYPE html><BR><html lang="en"><BR><head><BR> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"><BR> <script type="text/javascript" src="<SPAN style="COLOR: #ff0000"><STRONG>jquery.js</STRONG></SPAN>"></script></PRE>
<PRE class=brush> <script type="text/javascript"><BR> <SPAN style="COLOR: #ff0000"> $(document).ready(function(){</SPAN><BR><SPAN style="COLOR: #ff0000"> $("a").click(function(event){</SPAN><BR><SPAN style="COLOR: #ff0000"> alert("As you can see, the link no longer took you to jquery.com");</SPAN><BR><SPAN style="COLOR: #ff0000"> event.preventDefault();</SPAN><BR><SPAN style="COLOR: #ff0000"> });</SPAN><BR><SPAN style="COLOR: #ff0000"> });</SPAN><BR> <BR> </script><BR></head><BR><body><BR> <a href="<A class="external free" href="http://jquery.com/">http://jquery.com/</A>">jQuery</a><BR></body><BR></html>

代码解释:
  $(document).ready(function(){...})在页面加载完时添加function()函数内容
  $("a").click(function(event){...})设置a标签的click事件函数
  event.preventDefault()阻止原事件执行
  代码功能:点击<a>标签只弹出alert信息后,页面并不跳转到http://jquery.com/。
2. 添加和移除HTML class
  首先在<head>中添加一些样式,例如:
复制代码 代码如下:

<style type="text/css">
a.test { font-weight: bold; }
</style>

在script中使用addClass和removeClass来添加和移除HTML class,例如:
复制代码 代码如下:

$("a").addClass("test");//所有a标记粗体
$("a").removeClass("test");//取消所有a标记粗体

3.特效
  jQuery提供了一些非常方便的特效
复制代码 代码如下:

$("a").click(function(event){
event.preventDefault();
$(this).hide("slow");
});

点击<a>标签后,标记慢慢消失
4.回调与函数
  无参数回调
复制代码 代码如下:

$.get('myhtmlpage.html', myCallBack);

带参数回调
复制代码 代码如下:

$.get('myhtmlpage.html', function(){
myCallBack(param1, param2);
});
1. 使用jquery
  到jquery.com下载jquery.js当前版本是1.4.2
  新建一个html页面
复制代码 代码如下:
<!DOCTYPE html><BR><html lang="en"><BR><head><BR> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"><BR> <script type="text/javascript" src="<SPAN style="COLOR: #ff0000"><STRONG>jquery.js</STRONG></SPAN>"></script></PRE>
<PRE class=brush> <script type="text/javascript"><BR> <SPAN style="COLOR: #ff0000"> $(document).ready(function(){</SPAN><BR><SPAN style="COLOR: #ff0000"> $("a").click(function(event){</SPAN><BR><SPAN style="COLOR: #ff0000"> alert("As you can see, the link no longer took you to jquery.com");</SPAN><BR><SPAN style="COLOR: #ff0000"> event.preventDefault();</SPAN><BR><SPAN style="COLOR: #ff0000"> });</SPAN><BR><SPAN style="COLOR: #ff0000"> });</SPAN><BR> <BR> </script><BR></head><BR><body><BR> <a href="<A class="external free" href="http://jquery.com/">http://jquery.com/</A>">jQuery</a><BR></body><BR></html>

代码解释:
  $(document).ready(function(){...})在页面加载完时添加function()函数内容
  $("a").click(function(event){...})设置a标签的click事件函数
  event.preventDefault()阻止原事件执行
  代码功能:点击<a>标签只弹出alert信息后,页面并不跳转到http://jquery.com/。
2. 添加和移除HTML class
  首先在<head>中添加一些样式,例如:
复制代码 代码如下:
<style type="text/css">
a.test { font-weight: bold; }
</style>

在script中使用addClass和removeClass来添加和移除HTML class,例如:
复制代码 代码如下:
$("a").addClass("test");//所有a标记粗体
$("a").removeClass("test");//取消所有a标记粗体

3.特效
  jQuery提供了一些非常方便的特效
复制代码 代码如下:
$("a").click(function(event){
event.preventDefault();
$(this).hide("slow");
});

点击<a>标签后,标记慢慢消失
4.回调与函数
  无参数回调
复制代码 代码如下:
$.get('myhtmlpage.html', myCallBack);

带参数回调
复制代码 代码如下:
$.get('myhtmlpage.html', function(){
myCallBack(param1, param2);
});

« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3