JQuery②
- 1、jQuery的属性操作
- 2、JQuery练习
- 2、全选、全不选、反选
- 4、DOM的增删改
- 从左到右,从右到左.html
- 5、JQuery 练习2
- 动态添加、删除表格记录
- 6、CSs样式操作。
- 7、jQuery动画
- 基本动画
- 淡入淡出动画
- 六、练习品牌展示
- 8、jQuery事件操作
- jQuery中其他的事件处理方法:
- 事件的冒泡
- javaScript事件对象
- 七、练习图片跟随
1、jQuery的属性操作
jQuery属性操作
HTML代码/文本/值 html([val|fn]) text([val|fn]) val([val|fnlarr]
htm()
它可以设置或获取起始标签和结束标签中的内容
和dom属性innerHTML一样
text()
它可以设置或获取起始标签和结束标签中的文本
和dom属性innerHTML一样
test.html
<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="script/jquery.js"></script>
<script type="text/javascript">
$(function (){
//不传参数是获取,传递参数是设置
// alert($("div").html() );//获取
// $("div").html("<h1>我是div中的标题 1</h1>");//设置
//不传参数是获取,传递参数是设置
// alert($("div").text());//获取
// $("div").text("<h1>我是div中的标题 1</h1>");//设置
//不传参数是获取,传递参数是设置
$("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() 它可以设置或获取表单项的value属性值
和dom属性value一样
val方法同时设置多个表单项的值示例代码:
test2.html
<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="script/jquery.js"></script>
<script type="text/javascript">
$(function (){
/*
//批量操作单选
$(":radio").val(["radio2"]);
//批量操作筛选框中的选中状态
$(":checkbox").val(["checkbox3","checkbox2]);
//批量操作多选下拉框中的选中状态
$("#multiple").val(["mul2","mul3","mul4"]);
//批量操作单选下拉框中的选中状态
$("#single").val("sin2");
*/
$(":radio,#single,:checkbox,#multiple").val(["radio2","checkbox3","checkbox2","mul2","mul3","mul4","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="mull">mul1</option>
<option value="mul2">mul2</option>
<option value="mul3">mu13</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(namelpro|key,val|fn)
removeAttr(name)
prop(name|pro|key,val|fn)
removeProp(name)
attr()
可以设置和获取属性的值,
不推荐操作checked、readOnly、selected、disabled等等
attr方法还可以操作非标准的属性。比如自定义属性:abc、bbj
prop()
可以设置和获取属性的值,
只推荐操作checked、readOnly、selected、disabled等等
test3.html
<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="script/jquery.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"));//false
$(":checkbox:first").attr("abc","abcValue");
alert($(":checkbox:first").attr("abc"));
});
</script>
</head>
<body>
多选:
<input name="checkbox" type="checkbox" value="checkbox1" />checkbox1
<input name="checkbox" type="checkbox" value="checkbox2" />checkbox2
</body>
</html>
2、JQuery练习
2、全选、全不选、反选
ex-4.html
<!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.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 checkedCount=$(":checkbox[name='items']:checked").length;
// if (allCount==checkedCount){
// $("#checkedAllBox").prop("checked",true);
// }else {
// $("#checkedAllBox").prop("checked",false);
// }
$("#checkedAllBox").prop("checked",allCount==checkedCount);
});
//给提交按钮单击事件
$("#sendBtn").click(function () {
//获取选中的球类的复选框
var checkedCount=$(":checkbox[name='items']:checked").each(function () {
alert(this.value);
});
});
//给【全选/全不选】按钮绑定单击事件
$("#checkedAllBox").click(function () {
//在事件的function中有一个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>
4、DOM的增删改
内部插入
append(content|fn)
appendTo(content)
prepend(content|fn)
prependTo(content)
外部插入
after(content|fn)
before(content|fn)
insertAfter(content)
insertBefore(content]
替换
replacewith(content|fn)
replaceAl(selector)
删除
empty()
remove([expr])
内部插入:
appendTo()
a.appendTo(b)
把a插入到b子元素末属,成为最后一个子元素
prependTo/)
a.prependTo(b)
把a插到b所有子元素前面,成为第一个子元素
外部插入: insertAfter()
a.insertAfter(b)
得到ba
insertBefore()
a.insertBefore(b)
得到ab
替换: replaceWith()
a.replaceWith(b)
用b替换掉a
replaceAl()
a.replaceAl(b)
用a替换掉所有b
删除:
remove()
a.remove();
删除a标签
empty()
a.empty();
清空a际签里的内容
test3.html
<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="script/jquery.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"));//false
// $(":checkbox:first").attr("abc","abcValue");
// alert($(":checkbox:first").attr("abc"));
//-------------------------------------------
// $("<h1>标题</h1>").appendTo($("div"));
// $("<h1>标题</h1>").insertAfter($("div"));
// $("<h1>标题</h1>").insertBefore($("div"));
// $("div").replaceWith( $("<h1>标题</h1>") );
// $("<h1>标题</h1>").replaceAll("div");
// $("div").remove();
$("div").empty();
});
</script>
</head>
<body>
多选:
<input name="checkbox" type="checkbox" value="checkbox1" />checkbox1
<input name="checkbox" type="checkbox" value="checkbox2" />checkbox2
<br/>
<br/>
<div>1234</div>
<div>1234</div>
</body>
</html>
从左到右,从右到左.html
<!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.js"></script>
<script type="text/javascript">
//页面加载完成之后
$(function () {
//第一个按钮【选中添加到右边】
$("button:eq(0)").click(function () {
$("select:eq(0) option:selected").appendTo($("select:eq(1)"));
// $("select[name=sel01] :selected").each(function () { //alert(this);
// $(this).appendTo("select[name=sel02]");
// });
});
//第二个按钮【全部添加到右边】
$("button:eq(1)").click(function () {
$("select:eq(0) option").appendTo( $("select:eq(1)") );
// $("select[name=sel01] option").each(function () { //alert(this);
// $(this).appendTo("select[name=sel02]");
// });
});
//第三个按钮【选中删除到左边】
$("button:eq(2)").click(function () {
$("select:eq(1) option:selected").appendTo($("select:eq(0)"));
// $("select[name=sel02] :selected").each(function () { //alert(this);
// $(this).appendTo("select[name=sel01]");
// });
});
//第四个按钮【全部删除到左边】
$("button:eq(3)").click(function () {
$("select:eq(1) option").appendTo($("select:eq(0)"));
// $("select[name=sel02] option").each(function () { //alert(this);
// $(this).appendTo("select[name=sel01]");
// });
});
});
</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="right">
<select multiple="multiple" name="sel02">
</select>
<button>选中删除到左边</button>
<button>全部删除到左边</button>
</div>
</body>
</html>
5、JQuery 练习2
动态添加、删除表格记录
添加删除记录-5.html
<!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.js"></script>
<style type="text/css">
table{
width: 100px;
height: 140px;
border: 1px solid black;
border-collapse: collapse;
margin-left: auto;
margin-right: auto;
}
td,th{
border: 1px solid black;/*设置边框*/
}
div {
border: 1px black solid;
width: 300px;
height: 280px;
text-align: center;
margin-left: auto;
margin-right: auto;
}
</style>
<script type="text/javascript">
$(function () {
//创建一个复用的用于删除的函数
var deleteFun=function(){
// alert("删除事件"+this);
var $trObj=$(this).parent().parent();//this就是响应的dom对象
var name=$trObj.find("td:first").text();
/**
* confirm("");是JavaScript语言提供的一个确认提示框函数,你给它传什么,它就提示什么
* 当用户点击确定就返回true;当用户点击取消就返回false
*/
if(confirm("你确定要删除["+name+"]吗?")){
$trObj.remove();
}
// return false;可以阻止元素的默认行为
return false;
}
$("#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(function () {
// // alert("事件响应"+this);
// deleteFun();
// });
$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>
<br/>
<br/>
<br/>
<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>
6、CSs样式操作。
addClass
添加样式
moveClass)
删除样式
toggleClass[)
有就删除,没有就添加样式。
offset()
获取和设置元素的坐标。
jqueryCSS.html
<!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.js"></script>
<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">
$(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');
});
$("#btn04").click(function(){
//offset()-返回第一个匹配元素相对于文档的位置。
var pos=$divEle.offset();
// console.log(pos);
$divEle.offset({
top:100,
left:50
});
});
});
</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>
</body>
</html>
7、jQuery动画
基本动画
show()
将隐藏的元素显示
hide()
将可见的元素隐藏。
toggle()
可见就障藏,不可见就显示。
以上动画方法都可以添加参数。
1、第一个参数是动画执行的时长,以毫秒为单位
2、第二个参数是动画的回调函数(动画完成后自动调用的函数
淡入淡出动画
fadeln()
淡入(慢慢可见)
fadeOut(
淡出(慢慢消失)
fadeTo()
在指定时长内慢慢的将透明度修改到指定的值。0透明;且完成可见,0.5半透明
fadeToggle()
淡入/淡出切换
动画.html
<!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.js"></script>
<style type="text/css">
div{
width:100px;
height:260px;
border: 2px white solid;
background-color: red;
}
</style>
<script type="text/javascript">
$(function () {
//显示 show()
$("#btn1").click(function(){
$("#div1").show(2000,function () {
alert("show 动画已完成");
});
});
//隐藏 hide()
$('#btn2').click(function(){
$("#div1").hide(1000,function () {
alert("hide 动画已完成");
});
});
//切换 toggle()
$("#btn3").click(function(){
$("#div1").toggle(1000,function () {
alert("toggle 动画已完成");
});
});
// 前面用过的动画效果
// var abc=function(){
// $("#div1").toggle(1000, abc);
// }
//
// abc();
//淡入 fadeIn()
$("#btn4").click(function(){
$("#div1").fadeIn(1000,function () {
alert("fadeIn 动画已完成");
});
});
//淡出 fadeOut()
$("#btn5").click(function(){
$("#div1").fadeOut(2000,function () {
alert("fadeOut 动画已完成");
});
});
//淡化到 fadeTo()
$("#btn6").click(function(){
$("#div1").fadeTo(2000,0.5,function () {
alert("fadeTo 动画已完成");
});
});
//淡化切换(淡入/淡出) fadeToggle()
$("#btn7").click(function(){
$("#div1").fadeToggle(1000,function () {
alert("fadeToggle 动画已完成");
});
});
});
</script>
</head>
<body>
<table>
<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="...">
jquery定义了很多动画效果,可以很方便的使用这些动画效果
</div>
</body>
</html>
六、练习品牌展示
displaycs.html
<!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>品牌展示练习</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;
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.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显示所有的品牌 ==角标向下 showmore
$("div div a span").text("显示所有品牌 ");
$("div div").removeClass();
$("div div").addClass("showmore");
// 去高亮
$("li:contains('佳能')").removeClass("promoted");
}else {
// 品牌显示的状态 按钮上的文字 2显示精简的品牌 ==角标向上 showless
$("div div a span").text("显示精简品牌 ");
$("div div").removeClass();
$("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>
down.gif
up.gif
8、jQuery事件操作
$( function(){});
和
window.onload = function(){}
的区别?
- 他们分别是在什么时候触发?
1、jQuery的页面加载完成之后是浏览器的内核解析完页面的标签创建好DOM对象之后就会马上执行。
2、原生的js 的页面加载完成之后,除了要等浏览器的内核解析完页面的标签创建好DOM对象,还需要标签显示时需要的内容加载完成 - 他们触发的顺序?
1、jQuery页面加载完成之后先执行
2、原生的js 的页面加载完成之后 - 他们执行的次数
1、原生的js 的页面加载完成之后,只会执行最后一次的赋值函数
2、jQuery页面加载完成之后是把全部注册的fuction函数,依次顺序执行
<!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>I
<script type="text/javascript" src="../../script/jquery.js"></script>
<script type="text/javascript">
window.onload=function () {
alert("原生js的页面加载完成之后--1");
}
window.onload=function () {
alert("原生js的页面加载完成之后--2");
}
window.onload=function () {
alert("原生js的页面加载完成之后--3");
}
//全写
// $(document).ready(function () {
//
// });
//jquery的页面加载完成 之后
$(function () {
alert("jquery的页面加载完成之后--1");
});
$(function () {
alert("jquery的页面加载完成之后--2");
});
$(function () {
alert("jquery的页面加载完成之后--3");
});
</script>
</head>
<body>
<button>我是按钮</button>
<iframe src="http://www.baidu.com"></iframe>
<img src="http://www.baidu.com/1.jpn" alt=""/>
</body>
</html>>
jQuery中其他的事件处理方法:
click()
它可以绑定单击事件,以及触发单击事件
mouseover()
鼠标移入事件
mouseout()
鼠标移出事件
bind()
可以给元素一次性绑定一个或多个事件。
one()
使用上跟bind一样。但是one方法绑定的事件只会响应一次。
unbind()
跟bind方法相反的操作,解除事件的绑定
live()
也是用来绑定事件。它可以用来绑定选择器匹配的所有元素的事件。哪怕这个元素是后面动态创建出来的也有效
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.O1//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.js"></script>
<script type="text/javascript">
$(function(){
// $("h5").click(function () {//传function是绑定事件
// alert("h5单击事件==click方法绑定");
// });
//使用live绑定的单击事件
$("h5").live("click",function () {
alert("h5单击事件==live方法绑定");
});
$('<h5 class="head">什么是jQuery?</h5>').appendTo($("#panel"));
// $("button").click(function () {
// $("h5").click();
// });
//鼠标移入
// $("h5").mouseover(function () {
// console.log("你进来了");
// });
//鼠标移出
// $("h5").mouseout(function () {
// console.log("你出去了");
// });
//使用bind绑定事件
// $("h5").bind("click mouseover mouseout",function () {//空格隔开
// console.log("这是bind绑定的事件");
// });
//使用one绑定事件
// $("h5").one("click mouseover mouseout",function () {//空格隔开
// console.log("这是one绑定的事件");
// });
//使用unbind取消绑定事件
// $("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凭借简洁的
</div>
<button>按钮</button>
</div>
</body>
</html>
事件的冒泡
什么是事件的冒泡?
事件的冒泡是指,父子元素同时监听同一个事件。当触发子元素的事件的时候,同一个事件也被传递到了父元素的事件里去 响应。
那么如何阻止事件冒泡呢?
在子元素事件函数体内,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="../../script/jquery.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>
javaScript事件对象
事件对象,是封装有触发的事件信息的一个javascript对象。
我们重点关心的是怎么拿到这个javasript的事件对象。以及使用。
如何获取呢javascript事件对象呢?
在给元素绑定事件的时候,在事件的function(event)参数列表中添加一个参数,这个参数名,我们习惯取名为event. 这个event就是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同时对多个事件绑定同一个函数。怎么获取当前操作是什么事件。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.O1 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="../../script/jquery.js"></script>
<script type="text/javascript">
//1.原生javascript获取事件对象
// window.οnlοad=function () {
// document.getElementById("areaDiv").οnclick=function (event) {
// console.log(event);
// };
// }
//2.JQuery代码获取事件对象
$(function () {
// $("#areaDiv").click(function (event) {
// console.log(event);
// });
//3.使用bind同时对多个事件绑定同一个函数。怎么获取当前操作是什么事件?
$("#areaDiv").bind("mouseover mouseout",function (event) {
if (event.type=="mouseover"){
console.log("bind绑定的移入事件");
}else if (event.type=="mouseout"){
console.log("bind绑定的移出事件");
}
});
});
</script>
</head>
<body>
<div id="areaDiv"></div>
<div id="showMsg"></div>
</body>
</html>
七、练习图片跟随
<!DOCTYPE html PUBLIC "-//W3c//DTD HTML 4.O1 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.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.png">
<div id="showBig">
<img src="img/big.png">
</div>
</body>
</html>