17. jQuery 操作样式方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery</title>
</head>
<script src="public/jquery-1.9.1.min.js"></script>
<script>
$(function () {
$("div").css("width" , "100px");
$("div").css("height" , "100px");
$("div").css("background" , "red");
$("div").css("width" , "100px")
.css("height" , "100px")
.css("background" , "blue");
$("div").css({
width : "100px",
height : "100px",
background : "yellow"
});
console.log($("div").css("width"));
});
</script>
<body>
<div></div>
</body>
</html>
18. jQuery的尺寸和位置操作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery</title>
</head>
<script src="public/jquery-1.9.1.min.js"></script>
<style>
*{
margin: 0;
padding: 0;
}
.father{
width: 200px;
height: 200px;
background: red;
border: 50px solid #000;
margin-left: 50px;
position: relative;
}
.son{
width: 100px;
height: 100px;
background: blue;
left: 50px;
top: 50px;
position:absolute;
}
</style>
<script>
$(function () {
var btn = document.getElementsByTagName("button");
btn[0].onclick = function () {
alert($(".father").width());
alert($(".son").offset().left);
alert($(".son").position().left);
}
btn[1].onclick = function () {
$(".son").offset({
left:10
});
}
});
</script>
<body>
<div class="father">
<div class="son">
</div>
</div>
<button>获取</button>
<button>设置</button>
</body>
</html>
19.jQuery 的scrollTob方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery</title>
</head>
<script src="public/jquery-1.9.1.min.js"></script>
<style>
*{
margin: 0;
padding: 0;
}
.scroll{
width: 200px;
height: 200px;
border: 1px solid #000;
overflow: auto;
}
</style>
<script>
$(function () {
var btn = document.getElementsByTagName("button");
btn[0].onclick = function () {
alert($("html").scrollTop()+$("body").scrollTop());
}
btn[1].onclick = function () {
alert($("html,body").scrollTop(500));
}
});
</script>
<body>
<div class="scroll">我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div
我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div
我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div
我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div我是div
</div>
<button>获取</button>
<button>设置</button>
<br>
<br>
<br>
<br>
<br>
</body>
</html>
20. jQuery事件绑定
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery</title>
</head>
<script src="public/jquery-1.9.1.min.js"></script>
<script>
$(function () {
$("button").on("click",function () {
alert("hello")
})
$("button").on("click",function () {
alert("hello333")
})
$("button").on("mouseleave",function () {
alert("hello4444")
})
$("button").on("mouseenter",function () {
alert("hello66666")
})
});
</script>
<body>
<button>我是按钮</button>
</body>
</html>
21. jQuery事件解绑
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery</title>
</head>
<script src="public/jquery-1.9.1.min.js"></script>
<script>
$(function () {
function text1() {
alert("hello")
}
function text2() {
alert("0000")
}
$("button").click(text1);
$("button").click(text2);
$("button").mouseleave(function () {
alert("1111")
})
$("button").mouseenter(function () {
alert("2222")
})
$("button").off("click",text1);
});
</script>
<body>
<button>我是按钮</button>
</body>
</html>
22. jQuery事件冒泡和默认行为
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery</title>
</head>
<script src="public/jquery-1.9.1.min.js"></script>
<style>
*{
margin: 0;
padding: 0;
}
.father{
width: 200px;
height: 200px;
background: red;
}
.son{
width: 100px;
height: 100px;
background: blue;
}
</style>
<script>
$(function () {
$("a").click(function (event) {
alert("弹出注册框");
event.preventDefault();
})
});
</script>
<body>
<div class="father">
<div class="son"></div>
</div>
<a href="http://www.baidu.com">注册</a>
<form action="http://www.taobao.com">
<input type="text">
<input type="submit">
</form>
</body>
</html>
23. jQuery自动事件触发
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery</title>
</head>
<script src="public/jquery-1.9.1.min.js"></script>
<style>
*{
margin: 0;
padding: 0;
}
.father{
width: 200px;
height: 200px;
background: red;
}
.son{
width: 100px;
height: 100px;
background: blue;
}
</style>
<script>
$(function () {
$(".son").click(function (event) {
alert("son");
})
$(".father").click(function () {
alert("father")
})
$("input[type='submit']").click(function () {
alert("submit")
})
$("span").click(function () {
alert("a")
})
$("span").trigger("click");
});
</script>
<body>
<div class="father">
<div class="son"></div>
</div>
<a href="http://www.baidu.com"><span>注册</span></a>
<form action="http://www.taobao.com">
<input type="text">
<input type="submit">
</form>
</body>
</html>
24. jQuery自定义事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery</title>
</head>
<script src="public/jquery-1.9.1.min.js"></script>
<style>
*{
margin: 0;
padding: 0;
}
.father{
width: 200px;
height: 200px;
background: red;
}
.son{
width: 100px;
height: 100px;
background: blue;
}
</style>
<script>
$(function () {
$(".son").on("myClick",function () {
alert("aaa")
})
$(".son").trigger("myClick");
});
</script>
<body>
<div class="father">
<div class="son"></div>
</div>
<a href="http://www.baidu.com"><span>注册</span></a>
<form action="http://www.taobao.com">
<input type="text">
<input type="submit">
</form>
</body>
</html>
25. jQuery事件命名空间
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery</title>
</head>
<script src="public/jquery-1.9.1.min.js"></script>
<style>
*{
margin: 0;
padding: 0;
}
.father{
width: 200px;
height: 200px;
background: red;
}
.son{
width: 100px;
height: 100px;
background: blue;
}
</style>
<script>
$(function () {
$(".son").on("click.zs",function () {
alert("aaa")
})
$(".son").on("click.ls",function () {
alert("bbb")
})
$(".son").trigger("click.zs");
});
</script>
<body>
<div class="father">
<div class="son"></div>
</div>
<a href="http://www.baidu.com"><span>注册</span></a>
<form action="http://www.taobao.com">
<input type="text">
<input type="submit">
</form>
</body>
</html>
26. jQuery事件命名空间面试题
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery</title>
</head>
<script src="public/jquery-1.9.1.min.js"></script>
<style>
*{
margin: 0;
padding: 0;
}
.father{
width: 200px;
height: 200px;
background: red;
}
.son{
width: 100px;
height: 100px;
background: blue;
}
</style>
<script>
$(function () {
$(".father").on("click.ls",function () {
alert("aaa")
})
$(".father").on("click",function () {
alert("ccc")
})
$(".son").on("click.ls",function () {
alert("bbb")
})
$(".son").trigger("click")
});
</script>
<body>
<div class="father">
<div class="son"></div>
</div>
<a href="http://www.baidu.com"><span>注册</span></a>
<form action="http://www.taobao.com">
<input type="text">
<input type="submit">
</form>
</body>
</html>
27. jQuery事件委托
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery</title>
</head>
<script src="public/jquery-1.9.1.min.js"></script>
<style>
</style>
<script>
$(function () {
$("button").click(function () {
$("ul").append("<li>我是新增li</li>")
})
$("ul").delegate("li","click",function () {
alert($(this).html())
})
});
</script>
<body>
<ul>
<li>我是第一个li</li>
<li>我是第二个li</li>
<li>我是第三个li</li>
</ul>
<button>新增li</button>
</body>
</html>
28. jQuery移入移出事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery</title>
</head>
<script src="public/jquery-1.9.1.min.js"></script>
<style>
*{
margin: 0;
padding: 0;
}
.father{
width: 200px;
height: 200px;
background: red;
}
.son{
width: 100px;
height: 100px;
background: blue;
}
</style>
<script>
$(function () {
$(".father").hover(function () {
console.log("father移入yichu了");
})
});
</script>
<body>
<div class="father">
<div class="son">
</div>
</div>
</body>
</html>
29. 电影排行榜(移入移出事件练习)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery</title>
</head>
<script src="public/jquery-1.9.1.min.js"></script>
<style>
*{
margin: 0;
padding: 0;
}
.box{
width: 300px;
height: 450px;
margin: 50px auto;
border: 1px solid #000;
}
.box>h1{
font-size: 20px;
line-height: 35px;
color: deeppink;
padding-left: 10px;
border-bottom: 1px dashed #ccc;
}
ul>li{
list-style: none;
padding: 5px 10px;
border: 1px dashed #ccc;
}
ul>li:nth-child(-n+3) span{
background: deeppink;
}
ul>li>span{
display: inline-block;
width: 20px;
height: 20px;
background: #ccc;
text-align: center;
line-height: 20px;
margin-right: 10px;
}
.content{
overflow: hidden;
margin-top: 5px;
display: none;
}
.content>img{
width: 80px;
height: 120px;
float: left;
}
.content>p{
width: 180px;
height: 120px;
float: right;
font-size: 12px;
line-height: 20px;
}
.current .content{
display: block;
}
</style>
<script>
$(function () {
$("li").hover(function () {
$(this).addClass("current")
},function () {
$(this).removeClass("current")
})
});
</script>
<body>
<div class="box">
<h1>电影排行榜</h1>
<ul>
<li><span>1</span>电影名称
<div class="content">
<img src="https://img0.baidu.com/it/u=3643222036,2137679457&fm=26&fmt=auto&gp=0.jpg">
<p>痞性十足的冷锋(吴京饰)屡屡惹祸,有人说他是流氓,是痞子, 也有人说他是英雄,是传奇,在一次行动中冷锋违抗军令打死了恐怖分子,要被开除出队,却意外得到了神秘部队战狼的接纳,但本想换个地方继续惹祸的他却跳进了另外一个深渊,冷傲的战狼副队长,擦出暧昧火花的性感女队长(余男饰),心计颇深腹黑的毒枭,枉死的队友,雇佣兵跨过边境线入侵中国,一切都使他陷入了麻烦中。一切都在考验这个团队的毒瘤冷锋,他不知该何去何从。</p>
</div>
</li>
<li><span>2</span>电影名称
<div class="content">
<img src="https://img0.baidu.com/it/u=3643222036,2137679457&fm=26&fmt=auto&gp=0.jpg">
<p>痞性十足的冷锋(吴京饰)屡屡惹祸,有人说他是流氓,是痞子, 也有人说他是英雄,是传奇,在一次行动中冷锋违抗军令打死了恐怖分子,要被开除出队,却意外得到了神秘部队战狼的接纳,但本想换个地方继续惹祸的他却跳进了另外一个深渊,冷傲的战狼副队长,擦出暧昧火花的性感女队长(余男饰),心计颇深腹黑的毒枭,枉死的队友,雇佣兵跨过边境线入侵中国,一切都使他陷入了麻烦中。一切都在考验这个团队的毒瘤冷锋,他不知该何去何从。</p>
</div>
</li>
<li><span>3</span>电影名称
<div class="content">
<img src="https://img0.baidu.com/it/u=3643222036,2137679457&fm=26&fmt=auto&gp=0.jpg">
<p>痞性十足的冷锋(吴京饰)屡屡惹祸,有人说他是流氓,是痞子, 也有人说他是英雄,是传奇,在一次行动中冷锋违抗军令打死了恐怖分子,要被开除出队,却意外得到了神秘部队战狼的接纳,但本想换个地方继续惹祸的他却跳进了另外一个深渊,冷傲的战狼副队长,擦出暧昧火花的性感女队长(余男饰),心计颇深腹黑的毒枭,枉死的队友,雇佣兵跨过边境线入侵中国,一切都使他陷入了麻烦中。一切都在考验这个团队的毒瘤冷锋,他不知该何去何从。</p>
</div>
</li>
<li><span>4</span>电影名称
<div class="content">
<img src="https://img0.baidu.com/it/u=3643222036,2137679457&fm=26&fmt=auto&gp=0.jpg">
<p>痞性十足的冷锋(吴京饰)屡屡惹祸,有人说他是流氓,是痞子, 也有人说他是英雄,是传奇,在一次行动中冷锋违抗军令打死了恐怖分子,要被开除出队,却意外得到了神秘部队战狼的接纳,但本想换个地方继续惹祸的他却跳进了另外一个深渊,冷傲的战狼副队长,擦出暧昧火花的性感女队长(余男饰),心计颇深腹黑的毒枭,枉死的队友,雇佣兵跨过边境线入侵中国,一切都使他陷入了麻烦中。一切都在考验这个团队的毒瘤冷锋,他不知该何去何从。</p>
</div>
</li>
<li><span>5</span>电影名称
<div class="content">
<img src="https://img0.baidu.com/it/u=3643222036,2137679457&fm=26&fmt=auto&gp=0.jpg">
<p>痞性十足的冷锋(吴京饰)屡屡惹祸,有人说他是流氓,是痞子, 也有人说他是英雄,是传奇,在一次行动中冷锋违抗军令打死了恐怖分子,要被开除出队,却意外得到了神秘部队战狼的接纳,但本想换个地方继续惹祸的他却跳进了另外一个深渊,冷傲的战狼副队长,擦出暧昧火花的性感女队长(余男饰),心计颇深腹黑的毒枭,枉死的队友,雇佣兵跨过边境线入侵中国,一切都使他陷入了麻烦中。一切都在考验这个团队的毒瘤冷锋,他不知该何去何从。</p>
</div>
</li>
</ul>
</div>
</body>
</html>
30. tab选项卡
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery</title>
</head>
<script src="public/jquery-1.9.1.min.js"></script>
<style>
*{
margin: 0;
padding: 0;
}
.box{
width: 440px;
height: 298px;
border: 1px solid #000;
margin: 50px auto;
}
.nav>li{
list-style: none;
width: 110px;
height: 50px;
background: orange;
text-align: center;
line-height: 50px;
float: left;
}
.nav>.current{
background: #ccc;
}
.content>li{
list-style: none;
display: none;
}
.content .show{
display: block;
}
.content>li>img{
width: 440px;
height: 248px;
}
</style>
<script>
$(function () {
$(".nav>li").mouseenter(function () {
$(this).addClass("current");
$(this).siblings().removeClass("current");
var index = $(this).index();
var li = $(".content>li").eq(index);
li.siblings().removeClass("show");
li.addClass("show");
})
});
</script>
<body>
<div class="box">
<ul class="nav">
<li class="current">Css</li>
<li>jQuery</li>
<li>java</li>
<li>C++</li>
</ul>
<ul class="content">
<li class="show"><img src="https://img1.baidu.com/it/u=253337507,2981296721&fm=26&fmt=auto&gp=0.jpg"></li>
<li><img src="https://img1.baidu.com/it/u=3694053003,1912698683&fm=26&fmt=auto&gp=0.jpg"></li>
<li><img src="https://img1.baidu.com/it/u=4268324981,3384091170&fm=26&fmt=auto&gp=0.jpg"></li>
<li><img src="https://img1.baidu.com/it/u=1977046168,368269341&fm=26&fmt=auto&gp=0.jpg"></li>
</ul>
</div>
</body>
</html>