1、jQuery 的属性操作
<!DOCTYPE html>
<html lang="zh_CN">
<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 () {
// 不传参数,是获取,传递参数是设置
// alert($("div").text());
// $("div").text("我是div标签呀!");
// 不传参数,是获取,传递参数是设置
// alert($("div").html());
// alert($("div").html("我是div标签! <span>我是div中的span</span>"));
// 不传参数,是获取,传递参数是设置
$("button").click(function () {
// alert($("#username").val());
$("#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="zh_CN">
<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").val(["radio2"]);
// //批量操作多选框的选中状态
// $(":checkbox").val(["checkbox1","checkbox3"]);
// //批量操作多选下拉框的选中状态
// $("#multiple").val(["mul2","mul1","mul3"]);
// //操作单选下拉框的选中状态
// $("#single").val(["sin2"]);
//组合选中
$("#single,:checkbox").val(["checkbox1","checkbox2","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>
attr() 可以设置和获取属性的值, 不推荐操作 checked、readOnly、selected、disabled 等等
attr 方法还可以操作非标准的属性。比如自定义属性:abc,bbj
prop() 可以设置和获取属性的值, 只推荐操作 checked、readOnly、selected、disabled 等等
<!DOCTYPE html>
<html lang="zh_CN">
<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 () {
//attr
// alert( $(":checkbox:first").attr("name") ); // 获取
$(":checkbox:first").attr("name","abc") ; // 设置
//alert( $(":checkbox").attr("checked") );//官方觉得undefined是一个错误
//alert( $(":checkbox").prop("checked") );//碰到checked推荐使用prop操作属性
$(":checkbox:first").attr("abc","abcValue");
alert($(":checkbox:first").attr("abc"));
});
</script>
</head>
<body>
多选:
<input name="checkbox" type="checkbox" checked="checked" value="checkbox1" />checkbox1
<input name="checkbox" type="checkbox" value="checkbox2" />checkbox2
<input name="checkbox" type="checkbox" value="checkbox3" />checkbox3
<br/>
</body>
</html>
2、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 () {
//在each遍历的function函数中,有一个this对象。这个this对象是当前正在遍历到的dom对象
this.checked = !this.checked;
});
//要检查是否满选
//先获取全部的球类个数
var allCount = $(":checkbox[name='items']").length;
//再获取选中的球类的个数
var checkedCount = $(":checkbox[name='items']:checked").length;
$("#checkedAllBox").prop("checked", allCount == checkedCount);
});
//提交单击事件
$("#sendBtn").click(function () {
$(":checkbox[name='items']:checked").each(function () {
alert(this.value);
});
});
//给【全选/全不选】绑定单击事件
$("#checkedAllBox").click(function () {
//在事件的function函数中,有一个this对象,这个this对象是当期那正在响应事件的dom对象。
//alert(this.checked);
$(":checkbox[name='items']").prop("checked",this.checked);
});
//给全部球类绑定单击事件
$(":checkbox[name='items']").click(function () {
//要检查是否满选
//先获取全部的球类个数
var allCount = $(":checkbox[name='items']").length;
//再获取选中的球类的个数
var checkedCount = $(":checkbox[name='items']:checked").length;
$("#checkedAllBox").prop("checked", allCount == checkedCount);
});
});
</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 的增删改
<!DOCTYPE html>
<html lang="zh_CN">
<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 () {
//a.appendTo(b) 把 a 插入到 b 子元素末尾,成为最后一个子元素
//$("<h1>标题1</h1>").appendTo("div");
//a.prependTo(b) 把 a 插到 b 所有子元素前面,成为第一个子元素
//$("<h1>标题1</h1>").prependTo("div");
//a.insertAfter(b) 得到 ba
//$("<h1>标题1</h1>").insertAfter("div");
//a.insertBefore(b) 得到 ab
//$("<h1>标题1</h1>").insertBefore("div");
//a.replaceWith(b) 用 b 替换掉 a
//$("div").replaceWith("<h1>标题1</h1>");
//a.replaceAll(b) 用 a 替换掉所有 b
//$("<h1>标题1</h1>").replaceAll("div");
//a.remove(); 删除 a 标签
//$("div").remove();
//a.empty(); 清空 a 标签里的内容
$("div").empty();
});
</script>
</head>
<body>
多选:
<input name="checkbox" type="checkbox" checked="checked" value="checkbox1" />checkbox1
<input name="checkbox" type="checkbox" value="checkbox2" />checkbox2
<input name="checkbox" type="checkbox" value="checkbox3" />checkbox3
<br/>
<div>div1</div>
<div>div2</div>
</body>
</html>
4、jQuery的联系二
1.DOM操作,从左到右,从右到左练习
<!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 () {
//(左边的option).appendTo(右边的select)
$("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>
</head>
<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>
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 () {
//创建一个用于复用的删除的function函数
var deleteFun = function () {
//在事件响应的function函数中有一个this对象,这个this对象就是当前正在响应事件的dom对象。
var $trObj = $(this).parent().parent();
var name = $trObj.find("td:first").text();
/**
* confirm是JavaScript语言提供的一个确认提示框函数。你给他传什么,它就提示什么,
* 当用户点击了确定,就返回true,当用户点击了取消,它就返回false
*/
if (confirm("你确定要删除【" + name + "】吗?")){
$trObj.remove();
}
//return false; 可以阻止 元素的默认行为(比方说:阻止a标签的跳转)
return false;
}
// 给【Submit】按钮绑定单击事件
$("#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=001\">Delete</a></td>" +
"</tr>");
//添加到显示数据的表格中
$trObj.appendTo("#employeeTable");
//给添加的 行 的a标签绑定删除单击事件
$trObj.find("a").click(deleteFun);
});
//给删除的a标签绑定事件
$("a").click(deleteFun);
});
</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、CSS 样式操作
<!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(){
//addClass() - 向被选元素添加一个或多个类
$divEle.addClass("redDiv blueBorder");
});
$('#btn02').click(function(){
//removeClass() - 从被选元素删除一个或多个类
$divEle.removeClass("redDiv blueBorder");
});
$('#btn03').click(function(){
//toggleClass() - 对被选元素进行添加/删除类的切换操作
$divEle.toggleClass("redDiv blueBorder");
});
$('#btn04').click(function(){
//offset() - 返回第一个匹配元素相对于当前屏幕的位置。
var pos = $divEle.offset();
console.log(pos);
$divEle.offset({
top:100,
left:200
});
});
})
</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 动画
基本动画
show() 将隐藏的元素显示
hide() 将可见的元素隐藏。
toggle() 可见就隐藏,不可见就显示。
以上动画方法都可以添加参数。
1、第一个参数是动画 执行的时长,以毫秒为单位
2、第二个参数是动画的回调函数 (动画完成后自动调用的函数)
淡入淡出动画
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">
/*
基本
show([speed,[easing],[fn]])
hide([speed,[easing],[fn]])
toggle([speed],[easing],[fn])
滑动
slideDown([spe],[eas],[fn])
slideUp([speed,[easing],[fn]])
slideToggle([speed],[easing],[fn])
淡入淡出
fadeIn([speed],[eas],[fn])
fadeOut([speed],[eas],[fn])
fadeTo([[spe],opa,[eas],[fn]])
fadeToggle([speed,[eas],[fn]])
*/
$(function(){
/**
* 以下动画方法都可以添加参数。 1、第一个参数是动画 执行的时长,以毫秒为单位 2、第二个参数是动画的回调函数 (动画完成后自动调用的函数)
* 注意:
* fadeTo()有三个参数 1、第一个参数是动画 执行的时长,以毫秒为单位 2、在指定时长内慢慢的将透明度修改到指定的值。0 透明,1 完成可见,0.5 半透明
* 3、第二个参数是动画的回调函数 (动画完成后自动调用的函数)
*/
//显示 show()
$("#btn1").click(function(){
$("#div1").show(1000,function () {
alert("show完成");
});
});
//隐藏 hide()
$("#btn2").click(function(){
$("#div1").hide(1000);
});
//切换 toggle()
$("#btn3").click(function(){
$("#div1").toggle(800);
});
//淡入 fadeIn()
$("#btn4").click(function(){
$("#div1").fadeIn(1000);
});
//淡出 fadeOut()
$("#btn5").click(function(){
$("#div1").fadeOut(1000);
});
//淡化到 fadeTo()
$("#btn6").click(function(){
$("#div1").fadeTo(1000,0.6,function () {
alert("fadeTo完成");
});
});
//淡化切换 fadeToggle()
$("#btn7").click(function(){
$("#div1").fadeToggle(1000);
});
})
</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>
练习 CSS_动画 品牌展示
<!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")){
//按钮上的文本:1显示全部品牌 == 角标向下
$("div div a span").text("显示全部品牌");
$("div div").removeClass();
$("div div").addClass("showmore");
//去掉高亮
$("div li").removeClass("promoted");
}else {
//按钮上的文本:2显示精简品牌 == 角标向上
$("div div a span").text("显示精简品牌");
$("div div").removeClass();
$("div div ").addClass("showless");
//加高亮
$("div 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>
7、jQuery 事件操作
7.1 $( function(){} );** 和 **window.onload = function(){} 的区别?
答:
他们分别是在什么时候触发?
1、**jQuery的页面加载完成之后** 是浏览器的内核解析完页面的标签创建好DOM对象之后就会马上执行。
2、**原生js的页面加载完成之后**,除了要等浏览器内核解析完页面的标签创建好的DOM对象,还要能标签显示时需要的内容加载完成。
他们的触发顺序?
1、jQuery页面加载完成之后先执行。
2、原生js的页面加载完成之后 后执行
他们的执行次数?
1、原生js的页面加载完成之后,只会执行最后一次的赋值函数。
2、jQuery的页面加载完成之后是把 全部注册的function函数依次全部执行。
7.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(){
// //1.click事件
// $("h5").click(function () {
// alert("h5单击事件====click方法绑定");
// });
//7.使用live给某个标签绑定某个事件(例如单击事件),可以让后来创建的标签也自动绑定上该单击事件
$("h5").live("click",function () {
alert("h5单击事件====live方法绑定");
});
$('<h5 class="head">什么是jQuery</h5>').appendTo($("#panel"));
//
// $("button").click(function () {
// $("h5").click(); //不传function是触发事件。
// });
//
// //2.鼠标移入事件
// $("h5").mouseover(function () {
// console.log("鼠标进来了");
// });
//
// //3.鼠标移出事件
// $("h5").mouseout(function () {
// console.log("鼠标出来了");
// });
// //4.bind绑定多个事件,事件之间用空格隔开,每个事件可以响应多次
// $("h5").bind("click mouseover mouseout", function () {
// console.log("这是bind绑定的事件");
// });
// //5.one(),使用跟bind基本一样,只是one方法绑定的每个事件只能响应一次
// $("h5").one("click mouseover mouseout", function () {
// console.log("这是one绑定的事件");
// });
// //6.unbind用法与bind相反,它是解除多个事件
// $("h5").unbind("mouseover mouseout");
});
</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>
7.3 事件的冒泡
什么是事件的冒泡(通常我们需要阻止这种事件)?
事件的冒泡是指,父子元素同时监听同一个事件。当触发子元素的事件的时候,同一个事件也被传递到了父元素的事件里去 响应。
那么如何阻止事件冒泡呢?
在子元素事件函数体内,return false; 可以阻止事件的冒泡传递。
<!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(){
/**
* 事件的冒泡是指,父子元素同时监听同一个事件。当触发子元素的事件的时候,同一个事件也被传递到了父元素的事件里去 响应。
* 我们一般要阻止事件的冒泡传递,在子元素事件函数体内,return false; 可以阻止事件的冒泡传递。
*/
$("#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>
7.4 javaScript 事件对象
1.原生 javascript 获取 事件对象
window.onload = function () {
document.getElementById("areaDiv").onclick = function (event) {
console.log(event);
}
}
2.jQuery 代码获取 事件对象
$(function () {
$("#areaDiv").click(function (event) {
console.log(event);
});
});
3.使用 bind 同时对多个事件绑定同一个函数。怎么获取当前操作是什么事件。
$(function () {
$("#areaDiv").bind("mouseover mouseout",function (event) {
if (event.type == "mouseover"){
console.log("鼠标移入");
}else if (event.type == "mouseout"){
console.log("鼠标移出");
}
});
});
完整代码:
<!DOCTYPE html >
<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">
// //1.原生javascript获取 事件对象
// window.onload = function () {
// document.getElementById("areaDiv").onclick = function (event) {
// console.log(event);
// }
// }
//
// //2.JQuery代码获取 事件对象
// $(function () {
// $("#areaDiv").click(function (event) {
// console.log(event);
// });
// });
//3.使用bind同时对多个事件绑定同一个函数。怎么获取当前操作是什么事件。
$(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>
7.5 ☆练习 事件 图片跟随
<!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"){
$("#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>