源码下载 链接:https://pan.baidu.com/s/1nFa9iW4XZGFx8b6R65XAYg 提取码:7xa1
-
JQuery学习
-
1.JQuery的封装原理
-
2.JQuery的选择器
-
3.JQuery操作元素属性
-
4.JQuery操作元素内容JQuery
-
5.JQuery操作元素样式
-
6.JQuery
-
7.JQuery
3.JQuery操作元素属性
1.JQuery的封装原理
<html>
<head>
<title>jquery的封装原理title>
<meta charset="UTF-8"/>
<script src="js/my.js" type="text/javascript" charset="utf-8">script>
<script type="text/javascript">
function test(){
alert("我是test");
}
var bjsxt=123;
function testA(){
function test2(){
test2.name="张三";
var n=999;
alert(bjsxt);
return n;
}
return test2;
}
script>
head>
<body>
<h3>jquery的封装原理h3>
<hr />
<input type="button" name="" id="" value="测试test" onclick="bjsxt.test()"/>
<ul>
<li>1、js的全局代码区只有一个,这样就会造成同名变量的值会被覆盖。li>
<li>2、使用对象封装,将代码封装到对象中.但是对象如果被覆盖,则全部失效,风险极高。li>
<li>3、使用工厂模式,将代码进行封装,但是并没有解决问题li>
<li>4、将封装的函数名字去除,避免覆盖。但是函数没有办法调用了。li>
<li>5、匿名自调用,可以在页面加载的时候调用一次。但是不能重复调用,并且数据没有办法获取li>
<li>6、使用闭包,将数据一次性挂载到window对象下li>
ul>
body>
html>
2.JQuery的选择器
<html>
<head>
<title>jquery的选择器title>
<meta charset="UTF-8"/>
<script src="js/jquery-1.9.1.js" type="text/javascript" charset="utf-8">script>
<script type="text/javascript">
function testId(){
var inp=$("#uname");
alert(inp.val());
}
function testEle(){
var inps=$("input");
alert(inps[1].value);
}
function testClass(){
var inps=$(".common");
alert(inps.length);
}
function testAll(){
var eles=$("h3,input");
alert(eles.length);
}
function testChild(){
var inps=$("#showdiv>input");
alert(inps.length);
}
function testCj(){
var inp=$("input:first");
alert(inp.val());
}
function testCj2(){
var tds=$("td:not(td[width])");
alert(tds.html());
}
script>
<style type="text/css">
.common{}
div{
width: 300px;
height: 100px;
border: solid 2px orange;
}
style>
head>
<body>
<h3>jquery的选择器h3>
<input type="button" name="" id="" value="测试id" onclick="testId()" class="common"/>
<input type="button" name="" id="" value="测试标签选择器" onclick="testEle()"/>
<input type="button" name="" id="" value="测试类选择器" onclick="testClass()"/>
<input type="button" name="" id="" value="测试组合选择器" onclick="testAll()"/>
<hr />
用户名: <input type="text" name="uname" id="uname" value="张三" class="common"/>
<hr />
<input type="button" name="" id="" value="测试子选择器" onclick="testChild()" />
<input type="button" name="" id="" value="测试层级选择器--first" onclick="testCj()" />
<input type="button" name="" id="" value="测试层级选择器--not" onclick="testCj2()" />
<hr />
<div id="showdiv">
<input type="text" name="" id="" value="" />
<input type="text" name="" id="" value="" />
<input type="text" name="" id="" value="" />
<input type="text" name="" id="" value="" />
div>
<div id="">
<input type="text" name="" id="" value="" />
<input type="text" name="" id="" value="" />
<input type="text" name="" id="" value="" />
<input type="text" name="" id="" value="" />
div>
<table border="1px" height="200px">
<tr>
<td width="100px">td>
<td width="100px">td>
<td width="100px">td>
tr>
<tr>
<td>1td>
<td>2td>
<td>3td>
tr>
<tr>
<td>4td>
<td>5td>
<td>6td>
tr>
table>
body>
html>
3.JQuery操作元素属性
<html>
<head>
<title>jQuery操作元素的属性title>
<meta charset="UTF-8"/>
<script src="js/jquery-1.9.1.js" type="text/javascript" charset="utf-8">script>
<script type="text/javascript">
function testField(){
var uname=$("#uname");
alert(uname.attr("type")+":"+uname.attr("value")+":"+uname.val());
}
function testField2(){
var uname=$("#uname");
uname.attr("type","button");
}
script>
head>
<body>
<h3>jquery操作元素属性h3>
<input type="button" name="" id="" value="测试获取元素属性" onclick="testField()" />
<input type="button" name="" id="" value="测试修改元素属性" onclick="testField2()" />
<hr />
用户名:<input type="text" name="uname" id="uname" value="哈哈" />
body>
html>
4.JQuery操作元素内容
<html>
<head>
<title>操作元素HTMLtitle>
<meta charset="UTF-8"/>
<script src="js/jquery-1.9.1.js" type="text/javascript" charset="utf-8">script>
<script type="text/javascript">
function testHtml(){
var showdiv=$("#showdiv");
<!--
alert(showdiv.html());
}
function testHtml2(){
var showdiv=$("#showdiv");
showdiv.html(showdiv.html()+"今天天气真好,适合学习");
}
function testText(){
var showdiv=$("#showdiv");
alert(showdiv.text());
}
function testText2(){
var showdiv=$("#showdiv");
showdiv.text("今天天气真好,适合学习");
}
script>
head>
<body>
<h3>操作元素HTMLh3>
<input type="button" name="" id="" value="测试获取元素内容--html()" onclick="testHtml()" />
<input type="button" name="" id="" value="测试修改元素内容--html()" onclick="testHtml2()" />
<input type="button" name="" id="" value="测试获取元素内容--text()" onclick="testText()" />
<input type="button" name="" id="" value="测试修改元素内容--text()" onclick="testText2()" />
<div id="showdiv">
<b>昨天天气好,学习了一天b>
div>
body>
html>
5.JQuery操作元素样式
<html>
<head>
<title>操作元素样式title>
<meta charset="UTF-8"/>
<style type="text/css">
#showdiv{
width: 300px;
height: 300px;
border: solid 1px;
}
.common{
width: 300px;
height: 300px;
border: solid 1px;
background-color: blueviolet;
}
.dd{
font-size: 30px;
font-weight: bold;
}
style>
<script src="js/jquery-1.9.1.js" type="text/javascript" charset="utf-8">script>
<script type="text/javascript">
function testCss(){
var showdiv=$("#showdiv");
showdiv.css("background-color","orange");
alert(showdiv.css("width"));
}
function testCss2(){
var div=$("#div01");
div.css({"border":"solid 1px","width":"300px","height":"300px"});
}
function testAddClass(){
var div=$("#div01");
div.addClass("common");
}
function testAddClass2(){
var div=$("#div01");
div.addClass("dd");
}
function testRemoveClass(){
var div=$("#div01");
div.removeClass("dd");
}
script>
head>
<body>
<h3>操作元素样式h3>
<input type="button" name="" id="" value="操作样式---css()" onclick="testCss()" />
<input type="button" name="" id="" value="操作样式---css()--json" onclick="testCss2()" />
<input type="button" name="" id="" value="操作样式---addClass()" onclick="testAddClass()" />
<input type="button" name="" id="" value="操作样式---addClass()--2" onclick="testAddClass2()" />
<input type="button" name="" id="" value="操作样式---removeClass" onclick="testRemoveClass()" />
<hr />
<div id="showdiv">
div>
<div id="div01" class="common dd">
我是div01
div>
body>
html>
6.JQuery操作文档结构
<html>
<head>
<title>操作文档结构title>
<meta charset="UTF-8"/>
<script src="js/jquery-1.9.1.js" type="text/javascript" charset="utf-8">script>
<script type="text/javascript">
function testAppend(){
var div=$("#showdiv");
div.append(",饭");
}
function testAppendTo(){
var div=$("#showdiv");
$("#u1").appendTo(div);
}
function testPrepend(){
var div=$("#showdiv");
div.prepend("你好,");
}
function testAfter(){
var div=$("#showdiv");
div.after("今天天气不错,适合学习");
}
function testBefore(){
var div=$("#showdiv");
div.before("今天天气不错,适合学习")
}
function testEmpty(){
$("#showdiv").empty()
}
script>
<style type="text/css">
#showdiv{
width: 300px;
height: 300px;
border: solid 3px orange;
}
style>
head>
<body>
<h3>操作文档结构h3>
<input type="button" name="" id="" value="测试append" onclick="testAppend()" />
<input type="button" name="" id="" value="测试appenTo" onclick="testAppendTo()" />
<input type="button" name="" id="" value="测试prepend" onclick="testPrepend()" />
<hr />
<input type="button" name="" id="" value="测试after" onclick="testAfter()" />
<input type="button" name="" id="" value="测试before" onclick="testBefore()" />
<input type="button" name="" id="" value="测试删除--empty()" onclick="testEmpty()" />
<hr />
<u id="u1">每天下午都是充斥着面包浓浓的香味u>
<div id="showdiv">
<b>今天中午吃什么b>
div>
body>
html>
7.JQuery的事件机制
<html>
<head>
<title>操作事件title>
<meta charset="UTF-8"/>
<script src="js/jquery-1.9.1.js" type="text/javascript" charset="utf-8">script>
<script type="text/javascript">
function testThing(){
var btn=document.getElementById("btn2");
btn.onclick=function(){
alert("我是js方式");
}
}
function testBind(){
$("#btn2").bind("click",function(){alert("我是bind绑定单击事件")});
$("#btn2").bind("click",function(){alert("我是bind绑定单击事件2w2222")});
}
function testUnfBind(){
$("#btn3").unbind("click");
}
function testOne(){
$("#btn3").one("click",function(){alert("我是one")});
}
window.onload=function(){
alert("我是js方式页面加载");
}
window.onload=function(){
alert("我是js方式页面加载2222");
}
$(document).ready(function(){
alert("我是jQuery");
})
$(document).ready(function(){
alert("我是jQuery22222");
})
script>
head>
<body>
<h3>操作事件h3>
<input type="button" name="" id="" value="测试js动态添加事件" onclick="testThing()"/>
<input type="button" name="" id="" value="测试jquery动态添加事件--bind" onclick="testBind()"/>
<input type="button" name="" id="" value="测试jquery动态解绑事件--unbind" onclick="testUnfBind()"/>
<input type="button" name="" id="" value="测试jquery动态解绑事件--one" onclick="testOne()"/>
<hr />
<input type="button" name="" id="btn" value="测试js" />
<input type="button" name="btn2" id="btn2" value="测试jQuery-bind" />
<input type="button" name="btn2" id="btn3" value="测试jQuery-one" />
body>
html>
8.JQuery的动画效果
<html>
<head>
<title>动画效果title>
<meta charset="UTF-8"/>
<script src="js/jquery-1.9.1.js" type="text/javascript" charset="utf-8">script>
<style type="text/css">
#showdiv{
height: 300px;
background-color: orange;
display: none;
}
#div01{
height: 300px;
background-color:#8A2BE2;
}
style>
<script type="text/javascript">
function test(){
$("#showdiv").show(3000);
$("#div01").hide(3000);
$("div").toggle(3000);
$("#showdiv").slideDown(3000);
$("#div01").slideUp(2000);
$("#showdiv").fadeOut(3000);
}
script>
head>
<body onload="test()">
<div id="showdiv">
div>
<div id="div01">
div>
body>
html>
9.JQuery实战 一个动态菜单
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="js/jquery-1.9.1.js">script>
<style type="text/css">
body {
font-size: 16px;
background-color: #FFF;
}
ul li {
list-style: inside;
margin-left: 20px;
list-style: none;
}
#content {
position: absolute;
left: 400px;
top: 20px;
width: 500px;
height: 400px;
border: 1px solid #cccccc;
}
#tips {
position: absolute;
z-index: 1;
border: 1px solid #cccccc;
background-color: #FFC;
display: none;
font-size: 14px;
}
.lightlabel {
color: red;
text-decoration: underline;
cursor: pointer;
}
style>
<title>JQUERY树状菜单title>
<script type="text/javascript" src="script/jquery-1.11.0.min.js">script>
<script type="text/javascript">
$(function(){
$("#lblNews,#lblProducts").click(function(){
var containerDiv=$(this).siblings("div");
var img=$(this).siblings("#imgOnOff");
if(containerDiv.css("display")=="block"){
containerDiv.slideUp(1000);
img.attr("src","img/close.gif");
}else{
containerDiv.slideDown(1000);
img.attr("src","img/open.gif");
}
});
});
$(function(){
$("#openclose").click(function(){
var div= $("div[name*=Contain]");
var img=$("img[name='imgOnOff']");
if(div.css("display")=="block"){
div.fadeOut(1000);
img.attr("src","img/close.gif");
}else{
div.fadeIn(1000);
img.attr("src","img/open.gif");
}
});
});
$(function(){
$("#horder").click(function(){
var li= $("ul>div>li");
li.css("float","left");
});
$("#vorder").click(function(){
var li=$("ul li");
li.css("float","none");
});
});
$(function(){
$("ul div li label").mouseover(function(event){
var txt=$(this).text();
$("#tips").html("点击查看【"+txt+"】信息");
$("#tips").css("left",event.pageX+10);
$("#tips").css("top",event.pageY+5);
$("#tips").show();
$(this).addClass("lightlabel");
});
$("ul div li label").mouseout(function(){
$("#tips").html("");
$("#tips").css({left:"0","top":"0"});
$("#tips").hide();
$(this).removeClass("lightlabel");
});
});
$(function(){
$("div[name='divNewsContainer'] label").click(function(){
var ntype=$(this).attr("ntype");
$.ajax({
url:"news.action",
type:"get",
data:{"ntype":ntype},
dataType:"json",
success:function(result){
var content=$("#content");
content.empty();
for(var i=0;i<result.length;i++){
var news=result[i];
content.append(""+news.title+"("+news.time+")"+"
");
}
},
error:function(xhr,text,message){
console.info(xhr);
if(xhr.status==500){
alert("服务器异常,请联系管理员");
}
}
});
});
});
$(function(){
$("div[name='divProductsContainer'] label").click(function(){
var p=$(this).attr("product");
var url="product.action";
var data={"p":p};
$("#content").load(url,data);
});
});
script>
<body>
<p>
<input type="button" name="openclose" id="openclose" value="展开/折叠" />
<input type="button" name="horder" id="horder" value="横向排列" />
<input type="button" name="vorder" id="vorder" value="纵向排列" />
p>
<ul>
<img id="imgOnOff" name="imgOnOff" src="img/open.gif" />
<label id="lblNews"> 国际动态label>
<div name="divNewsContainer">
<li><img src="img/item.gif" /><label id="lblLocal" ntype="local">国内新闻label>li>
<li><img src="img/item.gif" /><label id="lblInternational" ntype="international">国际新闻label>li>
div>
ul>
<br />
<ul>
<img id="imgOnOff" name="imgOnOff" src="img/open.gif" />
<label id="lblProducts"> 产品展示label>
<div name="divProductsContainer">
<li><img src="img/item.gif" /><label id="lblAdidas" product="adidas">阿迪达斯label>li>
<li><img src="img/item.gif" /><label id="lblNike" product="nike">NIKElabel>li>
<li><img src="img/item.gif" /><label id="lblKuangWei" product="converse">匡威label>li>
<li><img src="img/item.gif" /><label id="lblAddNice" product="addnice">AddNicelabel>li>
<li><img src="img/item.gif" /><label id="lblLiNing" product="lining">李宁label>li>
<li><img src="img/item.gif" /><label id="lblLee" product="lee">Leelabel>li>
div>
ul>
<div id="tips">div>
<div id="content">div>
body>
html>