四、操作元素(属性,css,文档处理)
4.1属性操作
--------------------------属性
$("").attr();//拿到属性对应的值
$("").removeAttr();//
$("").prop();
$("").removeProp();
如果是固有的属性,就用prop,如果是自己定义的属性,就用attr
--------------------------CSS类
$("").addClass(class|fn)//增加一个class名字
$("").removeClass([class|fn])//删除一个class名字
--------------------------HTML代码/文本/值
$("").html([val|fn])
$("").text([val|fn])
$("").val([val|fn|arr])//取value值,必须固有value
---------------------------
$("").css("color","red")
$("").css({"color":"red","background":"green"})
eg:举例
<!DOCTYPE html>
<html>
<head>
<title>属性操作</title>
</head>
<body>
<div class="div1" con="c1 c2"></div>
<input type="checkbox" checked="checked">是否可见
<input type="checkbox">是否可见
<input type="text" value="123">
<div id="d1" value="456">
xjy
<p>hello</p>
</div>
</body>
<script src="E:/笔记/1.4web前端笔记/JQuery/jquery-3.1.1.js"></script>
<script type="text/javascript">
</script>
</html>
注意
<input id="chk1" type="checkbox" />是否可见
<input id="chk2" type="checkbox" checked="checked" />是否可见
<script>
console.log($("#chk1").prop("checked"));
console.log($("#chk2").prop("checked"));
console.log($("#chk1").attr("checked"));
console.log($("#chk2").attr("checked"));
</script>
实例之全反选(jquery)
<!DOCTYPE html>
<html>
<head>
<title>jquery正反选</title>
</head>
<body>
<button onclick="selectall();">全选</button>
<button onclick="cancel();">取消</button>
<button onclick="reverse();">反选</button>
<table border="1">
<tr>
<td><input type="checkbox"></td>
<td>111</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>222</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>333</td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>444</td>
</tr>
</table>
</body>
<script src="E:/笔记/1.4web前端笔记/JQuery/jquery-3.1.1.js"></script>
<script type="text/javascript">
function selectall(){
$(":checkbox").each(function(){
$(this).prop("checked",true)
})
}
function cancel(){
$(":checkbox").each(function(){
$(this).prop("checked",false)
})
}
function reverse(){
$(":checkbox").each(function(){
if($(this).prop("checked")){
$(this).prop("checked",false)
}
else{
$(this).prop("checked",true)
}
})
}
</script>
</html>
循环
<!DOCTYPE html>
<html>
<head>
<title>jquery的循环</title>
</head>
<body>
<p>11111</p>
<p>22222</p>
<p>33333</p>
</body>
<script src="E:/笔记/1.4web前端笔记/JQuery/jquery-3.1.1.js"></script>
<script type="text/javascript">
arr=[11,22,33]
</script>
</html>
实例之模态对话框
<!DOCTYPE html>
<html>
<head>
<title>模tai对话框jQuery</title>
<style>
.back{
background-color: rebeccapurple;
height: 2000px;
}
.shade{
position: fixed;
top: 0;
bottom: 0;
left:0;
right: 0;
background-color: coral;
opacity: 0.4;
}
.hide{
display: none;
}
.models{
position: fixed;
top: 50%;
left: 50%;
margin-left: -100px;
margin-top: -100px;
height: 200px;
width: 200px;
background-color: gold;
}
</style>
</head>
<body>
<div class="back">
<input id="ID1" type="button" value="click" onclick="action1(this)">
</div>
<div class="shade hide"></div>
<div class="models hide">
<input id="ID2" type="button" value="cancel" onclick="action2(this)">
</div>
</body>
<script src="E:/笔记/1.4web前端笔记/JQuery/jquery-3.1.1.js"></script>
<script>
function action1(self){
$(self).parent().siblings().removeClass("hide");
}
function action2(self){
$(self).parent().addClass("hide").prev().addClass("hide")
}
</script>
</html>
4.2文档处理
//创建一个标签对象
$("<p>")
//内部插入(插入子标签)
$("").append(content|fn) ----->$("p").append("<b>Hello</b>");//放在后面
$("").appendTo(content) ----->$("p").appendTo("div");
$("").prepend(content|fn) ----->$("p").prepend("<b>Hello</b>");//放在前面
$("").prependTo(content) ----->$("p").prependTo("#foo");
//外部插入(插入兄弟标签)
$("").after(content|fn) ----->$("p").after("<b>Hello</b>");//放在下面
$("").before(content|fn) ----->$("p").before("<b>Hello</b>");//放在上面
$("").insertAfter(content) ----->$("p").insertAfter("#foo");
$("").insertBefore(content) ----->$("p").insertBefore("#foo");
//替换
$("").replaceWith(content|fn) ----->$("p").replaceWith("<b>Paragraph. </b>");
//删除
$("").empty()
$("").remove([expr])
//复制
$("").clone([Even[,deepEven]])
eg:举例:
<!DOCTYPE html>
<html>
<head>
<title>文档处理</title>
</head>
<body>
<div class="c1">
<p>ppp</p>
</div>
<button>add</button>
</body>
<script src="E:/笔记/1.4web前端笔记/JQuery/jquery-3.1.1.js"></script>
<script type="text/javascript">
$("button").click(function(){
})
</script>
</html>
实例之复制样式条
<!DOCTYPE html>
<html>
<head>
<title>练习</title>
</head>
<body>
<div class="outer">
<div class="item">
<button onclick="add(this)">+</button>
<input type="text" name="">
</div>
</div>
</body>
<script src="E:/笔记/1.4web前端笔记/JQuery/jquery-3.1.1.js"></script>
<script type="text/javascript">
function add(wzq){
var $clone_obj=$(wzq).parent().clone();
$clone_obj.children("button").text("-").attr("onclick","remove_obj(this)")
$(".outer").append($clone_obj);
}
function remove_obj(self){
$(self).parent().remove()
}
</script>
</html>
4.3css操作
CSS
$("").css(name|pro|[,val|fn])
位置
$("").offset([coordinates])//offset()关于视口的偏移量
$("").position()
//position()关于已经设定定位的父标签的偏移量
$("").scrollTop([val])//上下滑轮
$("").scrollLeft([val])//左右滑轮
尺寸
$("").height([val|fn])
$("").width([val|fn])
$("").innerHeight()
$("").innerWidth()
$("").outerHeight([soptions])
$("").outerWidth([options])
eg:举例:
<!DOCTYPE html>
<html>
<head>
<title>css操作</title>
<style type="text/css">
*{
margin:0px;
padding: 0px;
}
.outer{
position: relative;
}
.div1, .div2{
width: 200px;
height: 100px;
}
.div1{
border:5px solid yellow;
padding: 20px;
margin: 2px;
background-color: red;
}
.div2{
background-color: green;
}
</style>
</head>
<body>
<div class="div1"></div>
<div class="outer">
<div class="div2"></div>
</div>
</body>
<script src="E:/笔记/1.4web前端笔记/JQuery/jquery-3.1.1.js"></script>
<script type="text/javascript">
</script>
</html>
4.3.1滑轮滚动事件和返回顶部
<!DOCTYPE html>
<html>
<head>
<title>滚动滑轮——返回顶部</title>
<style type="text/css">
*{
margin:0px;
padding: 0px;
}
.div2{
width: 100%;
height: 1000px;
background-color: green;
}
.div1{
width: 40%;
height: 400px;
background-color: red;
overflow: auto;
}
.returnTop{
position: fixed;
right: 20px;
bottom: 10px;
width: 100px;
height: 50px;
background-color: gray;
color: yellow;
text-align: center;
line-height: 50px;
}
.hide{
display: none;
}
</style>
</head>
<body>
<div class="div1">
<h1>wzq</h1>
<h1>wzq</h1>
<h1>wzq</h1>
<h1>wzq</h1>
<h1>wzq</h1>
<h1>wzq</h1>
<h1>wzq</h1>
<h1>wzq</h1>
<h1>wzq</h1>
<h1>wzq</h1>
<h1>wzq</h1>
<h1>wzq</h1>
</div>
<div class="div2">
<button onclick="returnTop2()">返回</button>
</div>
<div class="returnTop hide" onclick="returnTop()">返回顶部</div>
</body>
<script src="E:/笔记/1.4web前端笔记/JQuery/jquery-3.1.1.js"></script>
<script type="text/javascript">
window.onscroll=function(){
if ($(window).scrollTop()>100){
$(".returnTop").removeClass("hide")
}
else{
$(".returnTop").addClass("hide")
}
}
function returnTop(){
$(window).scrollTop(0);
}
function returnTop2(){
$(".div1").scrollTop(0);
}
</script>
</html>
五、事件
5.1页面载入
ready(fn) //当DOM载入就绪可以查询及操纵时绑定一个要执行的函数。
$(document).ready(function(){}) -----------> $(function(){})
5.2事件处理
$("").on(eve,[selector],[data],fn)
[selector]参数的好处:
好处在于.on方法为动态添加的元素也能绑上指定事件;如:
[data]参数的调用:
function myHandler(event) {
alert(event.data.foo);
}
$("li").on("click", {foo: "bar"}, myHandler)
<!DOCTYPE html>
<html>
<head>
<title>事件</title>
</head>
<body>
<ul>
<li>11</li>
<li>22</li>
<li>33</li>
<li>44</li>
<li>55</li>
</ul>
<button>add</button>
</body>
<script src="E:/笔记/1.4web前端笔记/JQuery/jquery-3.1.1.js"></script>
<script type="text/javascript">
</script>
</html>
5.3实例之面板拖动
<!DOCTYPE html>
<html>
<head>
<title>页面拖动</title>
<style type="text/css">
.div1{
border:1px solid #ddd;
width: 600px;
position: absolute;
}
.div2{
background-color: black;
height: 40px;
color: white;
}
.div3{
height: 30px;
}
</style>
</head>
<body>
<div class="div1">
<div id="title" class="div2">
标题
</div>
<div class="div3">
内容
</div>
</div>
</body>
<script src="E:/笔记/1.4web前端笔记/JQuery/jquery-3.1.1.js"></script>
<script type="text/javascript">
$(function(){
$('#title').mouseover(function(){
$(this).css('cursor','move');
}).mousedown(function(e){
var _event = e || window.event;
var ord_x = _event.clientX;
var ord_y = _event.clientY;
var parent_left = $(this).parent().offset().left;
var parent_top = $(this).parent().offset().top;
$(this).bind('mousemove', function(e){
var _new_event = e || window.event;
var new_x = _new_event.clientX;
var new_y = _new_event.clientY;
var x = parent_left + (new_x - ord_x);
var y = parent_top + (new_y - ord_y);
$(this).parent().css('left',x+'px');
$(this).parent().css('top',y+'px');
})
}).mouseup(function(){
$(this).unbind('mousemove');
});
})
</script>
</html>
5.4实例之放大镜
<!DOCTYPE html>
<html>
<head>
<title>放大镜</title>
<style>
*{
margin: 0;
padding:0;
}
.outer{
height: 350px;
width: 350px;
border: dashed 5px cornflowerblue;
}
.outer .small_box{
position: relative;
}
.outer .small_box .float{
height: 175px;
width: 175px;
background-color: darkgray;
opacity: 0.4;
fill-opacity: 0.4;
position: absolute;
display: none;
}
.outer .big_box{
height: 400px;
width: 400px;
overflow: hidden;
position:absolute;
left: 360px;
top: 0px;
z-index: 1;
border: 5px solid rebeccapurple;
display: none;
}
.outer .big_box img{
position: absolute;
z-index: 5;
}
</style>
</head>
<body>
<div class="outer">
<div class="small_box">
<div class="float"></div>
<img src="F://我鱼//1//-1c4f77f2262c4c10.jpg">
</div>
<div class="big_box">
<img src="F://我鱼//1//-4dce4e221befaa35.jpg">
</div>
</div>
</body>
<script src="E:/笔记/1.4web前端笔记/JQuery/jquery-3.1.1.js"></script>
<script type="text/javascript">
$(function(){
$(".small_box").mouseover(function(){
$(".float").css("display","block");
$(".big_box").css("display","block")
});
$(".small_box").mouseout(function(){
$(".float").css("display","none");
$(".big_box").css("display","none")
});
$(".small_box").mousemove(function(e){
var _event=e || window.event;
var float_width=$(".float").width();
var float_height=$(".float").height();
console.log(float_height,float_width);
var float_height_half=float_height/2;
var float_width_half=float_width/2;
console.log(float_height_half,float_width_half);
var small_box_width=$(".small_box").height();
var small_box_height=$(".small_box").width();
var mouse_left=_event.clientX-float_width_half;
var mouse_top=_event.clientY-float_height_half;
if(mouse_left<0){
mouse_left=0;
}else if (mouse_left>small_box_width-float_width){
mouse_left=small_box_width-float_width;
}
if(mouse_top<0){
mouse_top=0;
}else if (mouse_top>small_box_height-float_height){
mouse_top=small_box_height-float_height;
}
$(".float").css("left",mouse_left+"px");
$(".float").css("top",mouse_top+"px");
var percentX=($(".big_box img").width()-$(".big_box").width())/ (small_box_width-float_width);
var percentY=($(".big_box img").height()-$(".big_box").height())/(small_box_height-float_height);
console.log(percentX,percentY);
$(".big_box img").css("left", -percentX*mouse_left+"px");
$(".big_box img").css("top", -percentY*mouse_top+"px")
});
});
</script>
</html>