1.jQuery的属性操作
jQuery的常见属性操作 | 解释 |
---|
html() | 可以设置和获取起始标签和结束标签中的内容。跟dom属性innerHTML一样。 | text() | 可以设置起始标签和结束标签中的文本。跟dom属性innerText一样。 | val() | 可以设置和获取表单项的value属性值,跟dom属性value一样。 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="script/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function () {
$("button").click(function () {
$("#username").val("程序员");
});
});
</script>
</head>
<body>
<div>我是div标签 <span>我是div中的span</span></div>
<input type="text" name="username" id="username" >
<button>操作输入框</button>
</body>
</html>
同时需要注意上面的val()方法可以同时设置多个表单项的选中状态:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="script/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function(){
$(":radio,:checkbox,#multiple,#single").val(["radio2","checkbox2","checkbox1","mul2","mul3","sin2"])
});
</script>
</head>
<body>
单选:
<input name="radio" type="radio" value="radio1" />radio1
<input name="radio" type="radio" value="radio2" />radio2
<br/>
多选:
<input name="checkbox" type="checkbox" value="checkbox1" />checkbox1
<input name="checkbox" type="checkbox" value="checkbox2" />checkbox2
<input name="checkbox" type="checkbox" value="checkbox3" />checkbox3
<br/>
下拉多选 :
<select id="multiple" multiple="multiple" size="4">
<option value="mul1">mul1</option>
<option value="mul2">mul2</option>
<option value="mul3">mul3</option>
<option value="mul4">mul4</option>
</select>
<br/>
下拉单选 :
<select id="single">
<option value="sin1">sin1</option>
<option value="sin2">sin2</option>
<option value="sin3">sin3</option>
</select>
</body>
</html>
运行后的结果为: 
jQuery常见属性操作 | 解释 |
---|
attr() | 可以设置和获取属性的值,不推荐操作checked、readOnly、selected、disabled等 | prop() | 可以设置和获取属性的值,只推荐操作checked、readOnly、selected、disabled等 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="script/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function(){
$(":checkbox").prop("checked",true);
});
</script>
</head>
<body>
多选:
<input name="checkbox" type="checkbox" checked="checked" value="checkbox1" />checkbox1
<input name="checkbox" type="checkbox" value="checkbox2" />checkbox2
<br/>
</body>
</html>
运行后的结果为: 
2.jQuery练习:使用jQuery完成全选、全不选、反选和提交功能
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="../../script/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function(){
$("#checkedAllBtn").click(function () {
$(":checkbox").prop("checked",true);
});
$("#checkedNoBtn").click(function () {
$(":checkbox").prop("checked",false);
});
$("#checkedRevBtn").click(function () {
$(":checkbox[name='items']").each(function () {
this.checked=!this.checked;
});
var allCount = $(":checkbox[name='items']").length;
var checkCount = $(":checkbox[name='items']:checked").length;
$("#checkedAllBox").prop("checked",allCount==checkCount);
});
$("#sendBtn").click(function () {
$(":checkbox[name='items']:checked").each(function () {
alert(this.value);
});
});
$("#checkedAllBox").click(function () {
$(":checkbox[name='items']").prop("checked",this.checked);
});
$(":checkbox[name='items']").click(function () {
var allCount = $(":checkbox[name='items']").length;
var checkCount = $(":checkbox[name='items']:checked").length;
$("#checkedAllBox").prop("checked",allCount==checkCount);
})
});
</script>
</head>
<body>
<form method="post" action="">
你爱好的运动是?<input type="checkbox" id="checkedAllBox" />全选/全不选
<br />
<input type="checkbox" name="items" value="足球" />足球
<input type="checkbox" name="items" value="篮球" />篮球
<input type="checkbox" name="items" value="羽毛球" />羽毛球
<input type="checkbox" name="items" value="乒乓球" />乒乓球
<br />
<input type="button" id="checkedAllBtn" value="全 选" />
<input type="button" id="checkedNoBtn" value="全不选" />
<input type="button" id="checkedRevBtn" value="反 选" />
<input type="button" id="sendBtn" value="提 交" />
</form>
</body>
</html>
运行后的结果为:
全选  全不选  反选  提交  
3.DOM的增删改
DOM的增删改操作
内部插入
外部插入
替换
删除
3.1 DOM的增操作
3.1.1 内部插入
内部插入的方法 | 解释 |
---|
appendTo() | a.appendTo(b),把a插入到b所有子元素末尾,成为最后一个子元素 | prependTo() | a.prependTo(b),把a插入到b所有子元素前面,成为第一个子元素 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="script/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function(){
$("<h1>h1</h1>").prependTo("div");
});
</script>
</head>
<body>
<div>12345</div>
<div>1234</div>
</body>
</html>


3.1.2 外部插入
外部插入的方法 | 解释 |
---|
insertAfter() | a.insertAfter(b) ,得到ba | insertBefore() | a.insertBefore(b) ,得到ab |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="script/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function(){
$("<h1>h1</h1>").insertBefore("div");
});
</script>
</head>
<body>
<div>12345</div>
<div>1234</div>
</body>
</html>


3.2 DOM的改操作
替换的方法 | 解释 |
---|
replaceWith() | a.replaceWith(b),用b替换掉a | replaceAll() | a.replaceAll(b),用a替换掉所有b,注意有几个替换掉几个 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="script/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function(){
$("<h1>h1</h1>").replaceAll("div");
});
</script>
</head>
<body>
<div>12345</div>
<div>1234</div>
</body>
</html>
 
3.3 DOM的删操作
删除的方法 | 解释 |
---|
remove() | a.remove(),删除a标签 | empty() | a.empty(),清空a标签内的所有内容 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="script/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function(){
$("div").remove();
$("div").empty();
});
</script>
</head>
<body>
<div>12345</div>
<div>1234</div>
</body>
</html>
4.jQuery练习二
4.1 从左到右进行添加,从右往左进行删除
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
select {
width: 100px;
height: 140px;
}
div {
width: 130px;
float: left;
text-align: center;
}
</style>
<script type="text/javascript" src="script/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function () {
$("button:eq(0)").click(function(){
$("select:eq(0) option:selected").appendTo($("select:eq(1)"));
});
$("button:eq(1)").click(function(){
$("select:eq(0) option").appendTo($("select:eq(1)"));
});
$("button:eq(2)").click(function(){
$("select:eq(1) option:selected").appendTo($("select:eq(0)"));
});
$("button:eq(3)").click(function(){
$("select:eq(1) option").appendTo($("select:eq(0)"));
});
});
</script>
<body>
<div id="left">
<select multiple="multiple" name="sel01">
<option value="opt01">选项1</option>
<option value="opt02">选项2</option>
<option value="opt03">选项3</option>
<option value="opt04">选项4</option>
<option value="opt05">选项5</option>
<option value="opt06">选项6</option>
<option value="opt07">选项7</option>
<option value="opt08">选项8</option>
</select>
<button>选中添加到右边</button>
<button>全部添加到右边</button>
</div>
<div id="rigth">
<select multiple="multiple" name="sel02">
</select>
<button>选中删除到左边</button>
<button>全部删除到左边</button>
</div>
</body>
</html>
运行之后的结果为:    
4.2 动态添加、删除表格记录
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="styleB/css.css" />
<script type="text/javascript" src="../../script/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function () {
$("#addEmpButton").click(function () {
var name=$("#empName").val();
var email = $("#email").val();
var salary = $("#salary").val();
var $trObj = $("<tr>" +
"<td>" +name+ "</td>" +
"<td>" +email+ "</td>" +
"<td>" +salary+ "</td>" +
"<td><a href=\"deleteEmp?id=003\">Delete</a></td>" +
"</tr>");
$trObj.appendTo($("#employeeTable"));
$trObj.find("a").click(function () {
var $trObj= $(this).parent().parent();
var name=$trObj.find("td:first").text();
if(confirm("确定要删除["+ name +"]吗?")){
$trObj.remove();
}
return false;
})
});
$("a").click(function () {
var $trObj= $(this).parent().parent();
var name=$trObj.find("td:first").text();
if(confirm("确定要删除["+ name +"]吗?")){
$trObj.remove();
}
return false;
});
});
</script>
</head>
<body>
<table id="employeeTable">
<tr>
<th>Name</th>
<th>Email</th>
<th>Salary</th>
<th> </th>
</tr>
<tr>
<td>Tom</td>
<td>tom@tom.com</td>
<td>5000</td>
<td><a href="deleteEmp?id=001">Delete</a></td>
</tr>
<tr>
<td>Jerry</td>
<td>jerry@sohu.com</td>
<td>8000</td>
<td><a href="deleteEmp?id=002">Delete</a></td>
</tr>
<tr>
<td>Bob</td>
<td>bob@tom.com</td>
<td>10000</td>
<td><a href="deleteEmp?id=003">Delete</a></td>
</tr>
</table>
<div id="formDiv">
<h4>添加新员工</h4>
<table>
<tr>
<td class="word">name: </td>
<td class="inp">
<input type="text" name="empName" id="empName" />
</td>
</tr>
<tr>
<td class="word">email: </td>
<td class="inp">
<input type="text" name="email" id="email" />
</td>
</tr>
<tr>
<td class="word">salary: </td>
<td class="inp">
<input type="text" name="salary" id="salary" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<button id="addEmpButton" value="abc">
Submit
</button>
</td>
</tr>
</table>
</div>
</body>
</html>
运行后的结果为:
动态添加数据:  动态删除数据:  点击确认后成功删除数据: 
5.jQuery中CSS样式操作
jQuery中Css样式操作的常用方法 | 解释 |
---|
addClass() | 添加样式 | removeClass() | 删除样式 | toggleClass() | 有就删除,没有就添样式 | offest() | 获取和设置元素的坐标 |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
div{
width:100px;
height:260px;
}
div.whiteborder{
border: 2px white solid;
}
div.redDiv{
background-color: red;
}
div.blueBorder{
border: 5px blue solid;
}
</style>
<script type="text/javascript" src="script/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function(){
var $divEle = $('div:first');
$('#btn01').click(function(){
$divEle.addClass('redDiv blueBorder');
});
$('#btn02').click(function(){
$divEle.removeClass("blueBorder");
});
$('#btn03').click(function(){
$divEle.toggleClass("redDiv");
});
$('#btn04').click(function(){
var pos=$divEle.offset();
console.log(pos);
$divEle.offset({
top:15,
left:400
});
});
})
</script>
</head>
<body>
<table align="center">
<tr>
<td>
<div class="border">
</div>
</td>
<td>
<div class="btn">
<input type="button" value="addClass()" id="btn01"/>
<input type="button" value="removeClass()" id="btn02"/>
<input type="button" value="toggleClass()" id="btn03"/>
<input type="button" value="offset()" id="btn04"/>
</div>
</td>
</tr>
</table>
<br /> <br />
<br /> <br />
</body>
</html>
运行后的结果为:   
6.jQuery动画
jQuery中基本动画操作 | 解释 |
---|
show() | 将隐藏的元素显示 | hide() | 将可见的元素隐藏 | toggle() | 可见就隐藏,不可见就显示 |
以上动画都可以添加参数。
- 1.第一个参数是动画执行的时长,以毫秒为单位。
- 2.第二个参数是动画的回调函数(动画完成后自动调用的函数)。
jQuey中淡入淡出动画操作 | 解释 |
---|
fadeIn() | 淡入 (慢慢可见) | fadeOut() | 淡出(慢慢消失) | fadeTo | 在指定时间内慢慢将透明度修改到指定的值。0:透明,1:完全可见,0.5:半透明 | fadeToggle() | 淡入/淡出 切换 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Untitled Document</title>
<link href="css/style.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="script/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function(){
$("#btn1").click(function(){
$("#div1").show(1000,function () {
alert("show动画完成");
});
});
$("#btn2").click(function(){
$("#div1").hide(2000,function () {
alert("hide动画完成");
});
});
$("#btn3").click(function(){
$("#div1").toggle(1000,function () {
alert("toggle动画完成");
});
});
$("#btn4").click(function(){
$("#div1").fadeIn(5000,function () {
alert("fadeIn完成");
});
});
$("#btn5").click(function(){
$("#div1").fadeOut(2000,function () {
alert("fadeOut完成");
});
});
$("#btn6").click(function(){
$("#div1").fadeTo(1000,0.5,function () {
alert("fadeTo操作完成");
})
});
$("#btn7").click(function(){
$("#div1").fadeToggle(1000,function () {
alert("fadeToggle完成");
});
});
})
</script>
</head>
<body>
<table style="float: left;">
<tr>
<td><button id="btn1">显示show()</button></td>
</tr>
<tr>
<td><button id="btn2">隐藏hide()</button></td>
</tr>
<tr>
<td><button id="btn3">显示/隐藏切换 toggle()</button></td>
</tr>
<tr>
<td><button id="btn4">淡入fadeIn()</button></td>
</tr>
<tr>
<td><button id="btn5">淡出fadeOut()</button></td>
</tr>
<tr>
<td><button id="btn6">淡化到fadeTo()</button></td>
</tr>
<tr>
<td><button id="btn7">淡化切换fadeToggle()</button></td>
</tr>
</table>
<div id="div1" style="float:left;border: 1px solid;background-color: blue;width: 300px;height: 200px;">
jquery动画定义了很多种动画效果,可以很方便的使用这些动画效果
</div>
</body>
</html>
7.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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>品牌展示练习</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
body {
font-size: 12px;
text-align: center;
}
a {
color: #04D;
text-decoration: none;
}
a:hover {
color: #F50;
text-decoration: underline;
}
.SubCategoryBox {
width: 600px;
margin: 0 auto;
text-align: center;
margin-top: 40px;
}
.SubCategoryBox ul {
list-style: none;
}
.SubCategoryBox ul li {
display: block;
float: left;
width: 200px;
line-height: 20px;
}
.showmore , .showless{
clear: both;
text-align: center;
padding-top: 10px;
}
.showmore a , .showless a{
display: block;
width: 120px;
margin: 0 auto;
line-height: 24px;
border: 1px solid #AAA;
}
.showmore a span {
padding-left: 15px;
background: url(img/down.gif) no-repeat 0 0;
}
.showless a span {
padding-left: 15px;
background: url(img/up.gif) no-repeat 0 0;
}
.promoted a {
color: #F50;
}
</style>
<script type="text/javascript" src="script/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function() {
$("li:gt(5):not(:last) ").hide();
$("div div a").click(function () {
$("li:gt(5):not(:last)").toggle();
if($("li:gt(5):not(:last)").is(":hidden")){
$("div div a span").text("显示全部品牌");
$("div div").addClass("showmore");
$("div div").addClass("showmore");
$("li:contains('索尼')").addClass('promoted');
}else{
$("div div a span").text("显示精简品牌");
$("div div").addClass("showless");
$("li:contains('爱国者')").addClass("promoted");
}
return false;
})
});
</script>
</head>
<body>
<div class="SubCategoryBox">
<ul>
<li><a href="#">佳能</a><i>(30440) </i></li>
<li><a href="#">索尼</a><i>(27220) </i></li>
<li><a href="#">三星</a><i>(20808) </i></li>
<li><a href="#">尼康</a><i>(17821) </i></li>
<li><a href="#">松下</a><i>(12289) </i></li>
<li><a href="#">卡西欧</a><i>(8242) </i></li>
<li><a href="#">富士</a><i>(14894) </i></li>
<li><a href="#">柯达</a><i>(9520) </i></li>
<li><a href="#">宾得</a><i>(2195) </i></li>
<li><a href="#">理光</a><i>(4114) </i></li>
<li><a href="#">奥林巴斯</a><i>(12205) </i></li>
<li><a href="#">明基</a><i>(1466) </i></li>
<li><a href="#">爱国者</a><i>(3091) </i></li>
<li><a href="#">其它品牌相机</a><i>(7275) </i></li>
</ul>
<div class="showmore">
<a href="more.html"><span>显示全部品牌</span></a>
</div>
</div>
</body>
</html>
运行之后的结果为:  
8.jQuery事件操作
8.1 文档加载
$ (function(){});和window.οnlοad=function(){}的区别?
两者的触发顺序:
- 1.jQuery页面加载完成之后先执行。
- 2.原生的js的页面加载完成之后。
两者分别在什么时候触发?
- 1.jQuery的页面加载完成之后,是浏览器的内核解析完页面的标签创建好DOM对象之后就会马上执行。
- 2.原生js的页面加载完成之后,除了要等浏览器内核解析完标签创建好DOM对象,还要等标签显示时需要的内容加载完成。
两者执行的次数呢?
- 1.原生js的页面加载完成之后,只会执行最后一次的赋值函数。
- 2.jQuery的页面加载完成之后是全部把注册的functio()函数,依此顺序全部执行。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript"></script>
<script type="text/javascript" src="../../script/jquery-1.7.2.js"></script>
<script type="text/javascript">
window.onload=function () {
alert("原生js的页面加载完成之后--1");
};
window.onload=function () {
alert("原生js的页面加载完成之后--2");
};
window.onload=function () {
alert("原生js的页面加载完成之后--3");
};
$(function () {
alert("jQuery的页面加载完成之后--1");
});
$(function () {
alert("jQuery的页面加载完成之后--2");
});
$(function () {
alert("jQuery的页面加载完成之后--3");
});
</script>
</head>
<body>
<button>我是按钮</button>
<iframe src="http://www.baidu.com"></iframe>
</body>
</html>
8.2 常用事件处理方法
jQuery中 常用事件的处理方法 | 解释 |
---|
click() | 它可以绑定单击事件,以及可以触发单击事件 | mouseover() | 鼠标移入事件 | mouseout() | 鼠标移出事件 | bind() | 可以给元素一次性绑定一个或者多个事件 | one() | 使用上与bind一样。但是one方法绑定的事件只会响应一次 | unbind() | 与bind方法相反的操作,接触事件的绑定 | live() | 也是用来绑定事件。它可以用来绑定选择器匹配的所有元素的事件。哪怕这个元素是后面动态创建出来的也有效 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Untitled Document</title>
<link href="css/style.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../../script/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function(){
$("h5").bind("click mouseover ",function () {
console.log("这是bind绑定的事件");
});
$("h5").unbind("mouseover",function () {
console.log("这是unbind解绑的事件");
});
});
</script>
</head>
<body>
<div id="panel">
<h5 class="head">什么是jQuery?</h5>
<div class="content">
jQuery是继Prototype之后又一个优秀的JavaScript库,它是一个由 John Resig 创建于2006年1月的开源项目。jQuery凭借简洁的语法和跨平台的兼容性,极大地简化了JavaScript开发人员遍历HTML文档、操作DOM、处理事件、执行动画和开发Ajax。它独特而又优雅的代码风格改变了JavaScript程序员的设计思路和编写程序的方式。
</div>
<button>按钮</button>
</div>
</body>
</html>
8.3 事件的冒泡
什么是事件的冒泡? 事件的冒泡是指,父子元素同时监听同一个事件。当触发子元素的事件时,同一个事件也被传递到父元素的事件里去响应。
那么如何阻止事件的冒泡? 在子元素事件函数体内,return flase,就可以阻止事件的冒泡传递。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Untitled Document</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
body{
font-size: 13px;
line-height: 130%;
padding: 60px;
}
#content{
width: 220px;
border: 1px solid #0050D0;
background: #96E555;
}
span{
width: 200px;
margin: 10px;
background: #666666;
cursor: pointer;
color: white;
display: block;
}
p{
width: 200px;
background: #888;
color: white;
height: 16px;
}
</style>
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function(){
$("#content").click(function(){
alert("我是div");
});
$("span").click(function () {
alert("我是span");
return false;
});
})
</script>
</head>
<body>
<div id="content">
外层div元素
<span>内层span元素</span>
外层div元素
</div>
<div id="msg"></div>
<br><br>
<a href="http://www.hao123.com">WWW.HAO123.COM</a>
</body>
</html>
运行后的结果为: 
8.4 JavaScript 事件对象
事件对象,是装有触发的事件信息的一个JavaScript对象。
如何获取JavaScript事件对象呢? 在给元素绑定事件的时候,在事件的function(event)参数列表中添加一个参数,这个参数名我们通常称为event。这个event就是JavaScript传递参事件处理函数的事件对象。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
#areaDiv {
border: 1px solid black;
width: 300px;
height: 50px;
margin-bottom: 10px;
}
#showMsg {
border: 1px solid black;
width: 300px;
height: 20px;
}
</style>
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function () {
$("#areaDiv").bind("mouseover mouseout",function (event) {
if(event.type=="mouseover"){
console.log("鼠标移入");
}else if(event.type=="mouseout"){
console.log("鼠标移出");
}
});
});
</script>
</head>
<body>
<div id="areaDiv"></div>
<div id="showMsg"></div>
</body>
</html>
运行后的结果为: 
9.jQuery练习:图片跟随
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
body {
text-align: center;
}
#small {
margin-top: 150px;
}
#showBig {
position: absolute;
display: none;
}
</style>
<script type="text/javascript" src="script/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function(){
$("#small").bind("mouseover mouseout mousemove",function (event) {
if(event.type =="mouseover"){
$("#showBig").show();
}else if(event.type =="mouseout"){
$("#showBig").hide();
}else if (event.type =="mousemove"){
console.log(event);
$("#showBig").offset({
left:event.pageX+10,
top:event.pageY+10
});
}
})
});
</script>
</head>
<body>
<img id="small" src="img/small.jpg" />
<div id="showBig">
<img src="img/big.jpg">
</div>
</body>
</html>
运行后的结果为: 
总结
本节主要对jQuery中的属性操作、DOM的增删改、CSS样式操作、动画、事件操作作以介绍,这些操作都是页面操作中很重要的部分,需要重点掌握。 
|