1.操作页面元素的属性
//获得元素对象
var tex=$("#inp1");
//获得元素对象的属性
var te=tex.attr("type");
var cl =tex.attr("class");
//获得元素固有的属性值
var val =tex.attr("value");
console.log(te+"------"+cl+"-----"+val);
//获得文本框实时输入的值
var val2=tex.val();
// alert(val2);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript" src="js/jquery-1.9.1.js" ></script>
<script type="text/javascript">
$(function(){
$("#bu1").click(function(){
//获得元素对象
var tex=$("#zh1");
//获得元素对象的属性
var te=tex.attr("type");
var cl=tex.attr("class");
//获得元素固有的属性值
var val=tex.attr("value");
console.log(te+"---"+cl+"---"+val);
//获得文本框实时输入的值
var val2=tex.val();
// alert(val2);
/*********操作元素对象的属性******************/
tex.attr("type","button");
tex.attr("value","测试按钮");
//支持json数据格式
tex.attr({"type":"button","value":"测试按钮"});
// var ch=$("#fav").attr("checked");
var flag=$("#fav").prop("checked",true);
alert(flag);
})
})
</script>
</head>
<body>
<input type="button" name="" id="bu1" value="操作元素的属性" />
<hr />
<form>
账号:<input type="text" class="zh" id="zh1" value="sxt123" /><br />
密码:<input type="password" class="pwd" id="pwd1" value="sxt"/><br />
爱好: 抽烟:<input type="checkbox" />
喝酒:<input type="checkbox" />
烫头:<input type="checkbox" id="fav"/>
</form>
</body>
</html>



2.操作页面的文本和值
//获得div元素对象
var div =$("#div1");
//获得元素的内容 含有HTML的标签的
var ht=div.html();
console.log(ht);
//只是获得文本内容, 不含有HTML标签
var te =div.text();
console.log(te);
//获得文本框的值
var val=$("#inp1").val();
console.log(val);
}
可以识别里面的html代码
div.html("<b>我们都爱笑</b>");
识别不了里面的HTML代码
div.text(div.text()+"<b>我们都爱笑</b>");
获得文本的值
$("#inp1").val("sxt");
注意特殊情况: Select 、textarea 两个标签获得值得时候需要用val()
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript" src="js/jquery-1.9.1.js" ></script>
<script type="text/javascript">
$(function(){
$("#bu1").click(function(){
//获得div元素对象
var div=$("#div1");
//获得元素的内容 含有HTML的标签的
var ht=div.html();
console.log(ht);
//只是获得文本内容, 不含有HTML标签
var te=div.text();
console.log(te);
//获得文本框的值
var val=$("#div1").val();
console.log(val);
/********操作元素对象的内容和值****************/
div.html("<b>我们都爱笑</b>");
// div.text(div.text()+"<b>我们都爱笑</b>");
// $("#inp1").val("sxt");
// $("#inp1").val($("#inp1").val()+"sxt");
})
})
</script>
</head>
<body>
<input type="button" id="bu1" value="操作文本和值" />
<div id="div1">
<span>操作页面的文本和值</span>
</div>
<input type="text" id="inp1" name="inp1" value="" />
</body>
</html>


3.操作页面的元素
append(content)
概述
向每个匹配的元素内部追加内容。
这个操作与对指定的元素执行appendChild方法,将它们添加到文档中的情况类似。 appendTo(content)
概述
把所有匹配的元素追加到另一个指定的元素元素集合中。
实际上,使用这个方法是颠倒了常规的$(A).append(B)的操作,即不是把B追加到A中,而是把A追加到B中。
prepend(content) 返回值:jQuery
概述
向每个匹配的元素内部前置内容。
这是向所有匹配元素内部的开始处插入内容的最佳方式。
prependTo(content)
概述
把所有匹配的元素前置到另一个、指定的元素元素集合中。
实际上,使用这个方法是颠倒了常规的$(A).prepend(B)的操作,即不是把B前置到A中,而是把A前置到B中。 after(content)
概述
在每个匹配的元素之后插入内容。before(content)
概述
在每个匹配的元素之前插入内容。insertAfter(content)
概述
把所有匹配的元素插入到另一个、指定的元素元素集合的后面。
实际上,使用这个方法是颠倒了常规的$(A).after(B)的操作,即不是把B插入到A后面,而是把A插入到B后面。insertBefore(content)
概述
把所有匹配的元素插入到另一个、指定的元素元素集合的前面。
实际上,使用这个方法是颠倒了常规的$(A).before(B)的操作,即不是把B插入到A前面,而是把A插入到B前面。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
#div1{
height: 300px;
border: 3px solid blue;
}
p{
border: 2px solid yellow;
}
</style>
<script type="text/javascript" src="js/jquery-1.9.1.js" ></script>
<script type="text/javascript">
$(function(){
//创建新的元素
var p=$("<p> <b>List Itme6</b> </p>");
//增加子元素 ---现有元素之后
// $("#div1").append(p);
//把p元素增加到 div里面
// p.appendTo("#div1");
//添加内部的子元素 ---现有元素之前
// $("#div1").prepend(p);
// p.prependTo("#div1");
// $("#div1").before(p);
//平级的添加元素---现有元素之前
// p.insertBefore("#div1");
//平级的添加元素---现有元素之后
// $("#div1").after(p);
// p.insertAfter("#div1");
/***********替换指定的节点******************/
$("div p:nth-child(2)").replaceWith(p);
p.replaceAll("div p:nth-child(5)");
/**********删除指定的节点元素***************************/
//删除指定的元素
// $("#div1").remove();
// $("div p:nth-child(3)").remove();
//清空内容
// $("#div1").empty();
// $("div p:nth-child(2)").empty();
})
</script>
</head>
<body>
<div id="div1">
<p>List Itme1</p>
<p>List Itme2</p>
<p>List Itme3</p>
<p>List Itme4</p>
<p>List Itme5</p>
</div>
</body>
</html>

4.操作元素对象的节点
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript" src="js/jquery-1.9.1.js" ></script>
<script type="text/javascript">
function insertNode(){
//创建节点对象
var p=$('<p>'+'照片:<input type="file" name="" id=""/>'+'<input type="button" name="" id="" value="删除"
onclick="removeNode(this)"/>'+'</p>');
$("#last").before(p);
}
function removeNode(obj){
$(obj).parent().remove();
}
</script>
</head>
<body>
<form>
<p>
用户名:<input type="text" name="" id="" value=""/>
</p>
<p>
照片:<input type="file" name="" id="" value=""/>
<input type="button" name="" id="" value="添加" onclick="insertNode()"/>
</p>
<p id="last">
<input type="button" name="" id="" value="提交" />
<input type="button" name="" id="" value="清空" />
</p>
</form>
</body>
</html>


5.事件处理
单机事件click()
概述
触发每一个匹配元素的click事件。
这个函数会调用执行绑定到click事件的所有函数
双击事件 dblclick()
触发每一个匹配元素的dblclick事件。
这个函数会调用执行绑定到dblclick事件的所有函数,包括浏览器的默认行为。可以通过在某个绑定的函数中返回false来防止触发浏览器的默认行为。dblclick事件会在元素的同一点双击时触发。事件绑定bind(type, [data], fn)
概述
为每个匹配元素的特定事件绑定事件处理函数。
.bind() 方法是用于往文档上附加行为的主要方式。所有JavaScript事件对象,比如focus, mouseover, 和 resize,都是可以作为type参数传递进来的。
一次事件绑定one(type, [data], fn)
概述
为每一个匹配元素的特定事件(像click)绑定一个一次性的事件处理函数。trigger(type, [data])
概述
在每一个匹配的元素上触发某类事件。
这个函数也会导致浏览器同名的默认行为的执行。比如,如果用trigger()触发一个'submit',则同样会导致浏览器提交表单。如果要阻止这种默认行为,应返回false。 取消绑定 unbind(type,[data|fn]])
概述
bind()的反向操作,从每一个匹配的元素中删除绑定的事件。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="js/jquery-1.9.1.js" type="text/javascript" charset="UTF-8"></script>
<script type="text/javascript">
//页面加载完执行的操作
/*$(function(){});
jQuery(function(){});
$(document).ready(function(){})*/
$(function(){
//事件处理的基础绑定
// $("#bu1").click(function(){
// alert("单机事件绑定");
// })
$("#bu1").dblclick(function(){
alert("双击事件");
})
// $("#bu2").bind('click',function(){
// alert("bind事件绑定");
// })
//内容还支持json数据格式
$("#bu2").bind({
'click':function(){},
'dblclick':function(){},
'blur':function(){}
})
/*********one一次事件绑定*************/
$("#bu3").one("click",function(){
// alert("一次事件绑定");
})
/*********trigger事件操作****************/
// $("#bu4").click(function(){
// $("#bu1").trigger('dblclick');
// $("#bu3").trigger('click');
// })
/********事件的解绑***********/
$("#bu5").click(function(){
//事件的解绑
//解绑指定对象上的所有事件
// $("#bu1").unbind();
//解绑指定事件
$("#bu1").unbind('dblclick');
})
// $(".bu6").click(function(){
// alert("事件的操作");
// })
$(".bu6").live('click',function(){
alert("单击事件的绑定");
})
$("body").append("<input type='button' name='' id='bu6' value='事件操作2'/>")
})
</script>
</head>
<body>
<input type="button" name="bu1" id="bu1" value="事件绑定" />
<input type="button" name="bu2" id="bu2" value="bind事件绑定" />
<input type="button" name="bu3" id="bu3" value="one一次事件绑定" />
<input type="button" name="bu4" id="bu4" value="trigger事件绑定" />
<input type="button" name="bu5" id="bu5" value="事件解绑" />
<input type="button" name="" id="" class="bu6" value="事件操作" />
</body>
</html>
6.动画功能
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
#div1{
height: 300px;
background-color: aquamarine;
}
#div2{
height: 300px;
background-color: plum;
/*display: none;*/
}
</style>
<script type="text/javascript" src="js/jquery-1.9.1.js" ></script>
<script type="text/javascript">
$(function(){
$("#bu1").click(function(){
//获得div对象
var div1=$("#div1");
//3s的隐藏
// div1.hide(3000);
//3s的显示
// div1.show(3000);
//隐藏的显示 --显示的隐藏
// $("div").toggle(3000);
//滑动上 滑动下
// div1.slideDown(3000);
// div1.slideUp(3000);
//滑动上的滑动下 滑动下的滑动上
// $("div").slideToggle(3000);
//淡入淡出
// div1.fadeOut(3000);
// div1.fadeIn(3000);
$("div").fadeToggle(3000);
// $("div").fadeTo("slow",0.4);
})
})
</script>
</head>
<body>
<input type="button" id="bu1" value="动画触发" />
<div id="div1"></div>
<div id="div2"></div>
</body>
</html>

淡入淡出效果

7.jQuery封装的原理
闭包的优点:
1、可以减少全局变量的对象,防止全局变量过去庞大,导致难以维护
2、防止可修改变量,因为内部的变量外部是无法访问的,并且也不可修改的。安全
3、读取函数内部的变量,另一个就是让这些变量的值始终保持在内存中。
使用闭包的注意点
1)由于闭包会使得函数中的变量都被保存在内存中,内存消耗很大,所以不能滥用闭包,否则会造成网页的性能问题,在IE中可能导致内存泄露。解决方法是,在退出函数之前,将不使用的局部变量全部删除。
2)闭包会在父函数外部,改变父函数内部变量的值。所以,如果你把父函数当作对象(object)使用,把闭包当作它的公用方法(Public Method),把内部变量当作它的私有属性(private value),这时一定要小心,不要随便改变父函数内部变量的值。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
//无参数的匿名函数的执行
// (function(){
// alert("我是匿名函数");
// })()
//有参数匿名函数的执行
// (function(aa,bb,cc){
// alert("我是匿名函数"+aa+"--"+bb+"---"+cc);
// })(100,200,300)
// (function(){
// var a=100;
// window.vv=a;// 在运行的时候把a的值挂载到window对象上
// alert(a);
// })();
//
// function testBB(){
// alert(window.vv);
// alert(vv);
// }
var c=1000;
//在其他的方法里面也能访问局部的变量
function testA(){
var a=10;
function testB(){
alert(a);
var b=100;
return b;
}
var c=testB();
alert(c);
}
testA();
</script>
</head>
<body>
<h1>jQuery中的封装原理</h1>
<h3>匿名函数的自调用</h3>
<h3>闭包原理</h3>
<h4>
闭包的优点: <br />
1、可以减少全局变量的对象,防止全局变量过去庞大,导致难以维护<br />
2、防止可修改变量,因为内部的变量外部是无法访问的,并且也不可修改的。安全<br />
3、读取函数内部的变量,另一个就是让这些变量的值始终保持在内存中。 <br />
</h4>
<input type="button" value="测试" onclick="testBB()" />
</body>
</html>



8.操作购物车实例
html代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<!--声明css代码域-->
<style type="text/css">
tr{
height: 40px;
}
</style>
<script type="text/javascript" src="js/jquery-1.9.1.js" ></script>
<script src="js/gwc.js" type="text/javascript"></script>
</head>
<body>
<h3>jQuery操作表格</h3>
<hr />
<input type="button" id="fx" value="反选" />
<input type="button" id="addRow" value="新增一行" />
<input type="button" id="delRow" value="删除行" />
<input type="button" id="copyRow" value="复制行" />
<table border="1px" cellpadding="10px" cellspacing="0" id="ta">
<tr>
<td width="50px"><input type="checkbox" name="chks" id="chks" value="1" /></td>
<td width="200px">书名</td>
<td width="200px">作者</td>
<td width="200px">数量</td>
<td width="200px">操作</td>
</tr>
<tr id="">
<td><input type="checkbox" name="chk" id="" value="2"/></td>
<td>《Java编程之道》</td>
<td>wollo</td>
<td>10</td>
<td>
<input type="button" name="aa" id="" value="修改数量" onclick="change(this)"/>
<input type="button" name="" id="" value="删除" onclick="del(this)"/>
</td>
</tr>
<tr>
<td><input type="checkbox" name="chk" id="" value="3" /></td>
<td>《Python和我的故事》</td>
<td>赵老师</td>
<td>10</td>
<td>
<input type="button" name="" id="" value="修改数量" onclick="change(this)"/>
<input type="button" name="" id="" value="删除" onclick="del(this)"/>
</td>
</tr>
<tr>
<td><input type="checkbox" name="chk" id="" value="4" /></td>
<td>《web开发详解》</td>
<td>张老师</td>
<td>30</td>
<td>
<input type="button" name="" id="" value="修改数量" onclick="change(this)"/>
<input type="button" name="" id="" value="删除" onclick="del(this)"/>
</td>
</tr>
</table>
</body>
</html>
js代码
$(function(){
//确定全选和全不选的操作
$("#chks").click(function(){
var flag=$(this).prop("checked");
$("input[name=chk]").prop("checked",flag);
})
$("input[name=chk]").click(function(){
var flag=true;
var chk=$("input[name=chk]");
chk.each(function(){
if (!$(this).prop("checked")) {
flag=false;
return;
}
})
$("#chks").prop("checked",flag);
})
//反选的操作
$("#fx").click(function(){
var chx=$("input[type=checkbox]");
chx.each(function(){
//获得多选框的初始状态
var flag=$(this).prop("checked");
$(this).prop("checked",!flag);
})
})
//新增一行
$("#addRow").click(function(){
var tab=$("#ta");
tab.append('<tr id="">'+
'<td><input type="checkbox" name="chk" id="" value="2"/></td>'+
'<td>《Java编程之道》</td>'+
'<td>wollo</td>'+
'<td>10</td>'+
'<td>'+
'<input type="button" name="aa" id="" value="修改数量" />'+
'<input type="button" name="" id="" value="删除" />'+
'</td>'+
'</tr>')
})
//删除行
$("#delRow").click(function(){
var del=$("input[name=chk]:checked");
if (del.length==0) {
alert("至少选择一行!");
} else{
//执行删除的操作
del.parent().parent().remove();
}
})
//复制行的操作
$("#copyRow").click(function(){
var copy=$("input[name=chk]:checked");
if (copy.length==0) {
alert("至少选择一行!");
} else{
//执行copy
var tr=copy.parent().parent().clone();
//粘贴
$("#ta").append(tr);
}
})
})
//修改数量的操作
function change(th){
var par=$(th).parent().parent();
par.children().eq(3).html("<input type='text' size='3px' onblur='bul(this)'/>");
}
function bul(th){
var par=$(th).parent().parent();
par.children().eq(3).html(th.value);
}
//删除的操作
function del(th){
var par=$(th).parent().parent();
par.remove();
}
新增一行效果

删除行效果

复制行效果

修改数量
