Day57:操作元素(事件、属性、循环、文档处理)

时间:2022-10-15 05:43:33

一、操作元素(属性、CSS、文档处理)

1、事件

1.1 页面载入

ready(fn)  // 当DOM载入就绪可以查询及操纵时绑定一个要执行的函数。
$(document).ready(function(){}) -----------> $(function(){})

1.2 事件绑定

//语法:  标签对象.事件(函数)    
eg: $("p").click(function(){})

1.3 事件委派

$("").on(eve,[selector],[data],fn)  // 在选择元素上绑定一个或多个事件的事件处理函数。
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<hr>
<button id="add_li">Add_li</button>
<button id="off">off</button>

<script src="jquery.min.js"></script>
<script>
$(
"ul li").click(function(){
alert(
123)
});

$(
"#add_li").click(function(){
var $ele=$("<li>");
$ele.text(Math.round(Math.random()
*10));
$(
"ul").append($ele)

});


// $("ul").on("click","li",function(){
//
alert(456)
//
})

$(
"#off").click(function(){
$(
"ul li").off() //解除事件绑定
})

</script>

1.4 事件切换

  hover事件:

  一个模仿悬停事件(鼠标移动到一个对象上面及移出这个对象)的方法。这是一个自定义的方法,它为频繁使用的任务提供了一种“保持在其中”的状态。

  over:鼠标移到元素上要触发的函数

  out:鼠标移出元素要触发的函数

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{
margin:
0;
padding:
0;
}
.test{

width: 200px;
height: 200px;
background
-color: wheat;

}
</style>
</head>
<body>


<div class="test"></div>
</body>
<script src="jquery.min.js"></script>
<script>
// 使用方法// function enter(){
//
console.log("enter")
//
}
//
function out(){
//
console.log("out")
//
}
//
$(".test").hover(enter,out)


$(
".test").mouseenter(function(){
console.log(
"enter")
});

$(
".test").mouseleave(function(){
console.log(
"leave")
});

</script>
</html>

2、属性操作

--------------------------CSS类
$(
"").addClass(class|fn)
$(
"").removeClass([class|fn])

--------------------------属性
$(
"").attr();
$(
"").removeAttr();
$(
"").prop();
$(
"").removeProp();

--------------------------HTML代码/文本/
$(
"").html([val|fn])
$(
"").text([val|fn])
$(
"").val([val|fn|arr])

---------------------------
$(
"#c1").css({"color":"red","fontSize":"35px"} 

attr和prop的使用方法:

Day57:操作元素(事件、属性、循环、文档处理)Day57:操作元素(事件、属性、循环、文档处理)
<input id="chk1" type="checkbox" />是否可见
<input id="chk2" type="checkbox" checked="checked" />是否可见

<script>

//对于HTML元素本身就带有的固有属性,在处理时,使用prop方法。
//
对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法。
//
像checkbox,radio和select这样的元素,选中属性对应“checked”和“selected”,这些也属于固有属性,因此
//
需要使用prop方法去操作才能获得正确的结果。


// $("#chk1").attr("checked")
//
undefined
//
$("#chk1").prop("checked")
//
false

// ---------手动选中的时候attr()获得到没有意义的undefined-----------
//
$("#chk1").attr("checked")
//
undefined
//
$("#chk1").prop("checked")
//
true

console.log($(
"#chk1").prop("checked"));//false
console.log($("#chk2").prop("checked"));//true
console.log($("#chk1").attr("checked"));//undefined
console.log($("#chk2").attr("checked"));//checked
</script>
View Code

3、each循环

我们知道

$("p").css("color","red")

是将css操作加到所有的标签上,内部维持一个循环;但如果对于选中标签进行不同处理,这时就需要对所有标签数组进行循环遍历啦

jquery支持两种循环方式:

方式一:格式:$.each(obj,fn)

li=[10,20,30,40];
dic
={name:"yuan",sex:"male"};
$.each(li,
function(i,x){
console.log(i,x) // i为索引,x为元素
});

方式二:格式:$("").each(fn)

$("tr").each(function(){
console.log($(
this).html())
})

其中,$(this)代指当前循环标签。

each扩展:

/*
function f(){

for(var i=0;i<4;i++){

if (i==2){
return
}
console.log(i)
}
}
f(); // 这个例子大家应该不会有问题吧!!!
//-----------------------------------------------------------------------
li=[11,22,33,44];
$.each(li,function(i,v){

if (v==33){
return ; // ===试一试 return false会怎样?
}
console.log(v)
});
//------------------------------------------

// 大家再考虑: function里的return只是结束了当前的函数,并不会影响后面函数的执行

//本来这样没问题,但因为我们的需求里有很多这样的情况:我们不管循环到第几个函数时,一旦return了,
//希望后面的函数也不再执行了!基于此,jquery在$.each里又加了一步:
for(var i in obj){

ret=func(i,obj[i]) ;
if(ret==false){
return ;
}

}
// 这样就很灵活了:
// <1>如果你想return后下面循环函数继续执行,那么就直接写return或return true
// <2>如果你不想return后下面循环函数继续执行,那么就直接写return false


// ---------------------------------------------------------------------

4、文档节点处理

//创建一个标签对象
$("<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]])

 二、课后作业

Day57:操作元素(事件、属性、循环、文档处理)

Day57:操作元素(事件、属性、循环、文档处理)Day57:操作元素(事件、属性、循环、文档处理)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title</title>
<style>
*
{
margin
: 0;
padding
: 0;
}

.outer
{
width
: 790px;
height
: 330px;
margin
: 0px auto;
position
: relative;
top
: 30px;
}

.outer a
{
position
: absolute;
top
: 0;
left
: 0;
}

.bottom
{
position
: absolute;
bottom
: 10px;
left
: 37%;
}

.bottom p
{
display
: inline-block;
background-color
: grey;
text-align
: center;
opacity
: 0.7;
border-radius
: 10px;
margin
: 2px;
padding
: 0 5px;
}

.bottom p:hover
{
opacity
: 1;
color
: white;

}

.jia p
{
width
: 20px;
height
: 40px;
color
: white;
text-align
: center;
line-height
: 40px;
position
: absolute;
background-color
: #CFD0DE;
top
: 50%;
margin-top
: -40px;
opacity
: 0.3;
}

.right
{
right
: 0;
}

.jia p:hover
{
color
: #5a5456;
opacity
: 1;

}

.hide
{
display
: none;
}

.active
{
color
: white;
opacity
: 1;
}
</style>
</head>
<body>

<div class="outer">
<div class=" img"><a href="#"><img src="jpg/1.jpg" alt=""></a></div>
<div class=" img"><a href="#"><img src="jpg/2.jpg" alt=""></a></div>
<div class=" img"><a href="#"><img src="jpg/3.jpg" alt=""></a></div>
<div class=" img"><a href="#"><img src="jpg/4.jpg" alt=""></a></div>
<div class=" img"><a href="#"><img src="jpg/5.jpg" alt=""></a></div>
<div class=" img"><a href="#"><img src="jpg/6.jpg" alt=""></a></div>
<div class=" img"><a href="#"><img src="jpg/7.jpg" alt=""></a></div>
<div class=" img"><a href="#"><img src="jpg/8.jpg" alt=""></a></div>
<div class="bottom">
<p id="p1">1</p>
<p id="p2">2</p>
<p id="p3">3</p>
<p id="p4">4</p>
<p id="p5">5</p>
<p id="p6">6</p>
<p id="p7">7</p>
<p id="p8" class="active">8</p>
</div>
<div class="jia">
<p class="left">< </p>
<p class="right">> </p>
</div>
</div>
<script src="../jquery-3.2.1.js"></script>
<script>

var i = $(".img").length - 1;
var ID;
$(
".bottom p").mouseover(function () {
$(
".img").eq($(this).index()).removeClass("hide").siblings(".img").addClass("hide");
$(
this).addClass("active").siblings().removeClass("active")
i
= $(this).index()
})


function foo() {
if (i == $(".img").length - 1) {
i
= -1;
}
i
++;
$(
".img").eq(i).removeClass("hide").siblings(".img").addClass("hide");
$(
".bottom p").eq(i).addClass("active").siblings().removeClass("active")
}

ID
= setInterval(foo, 1000);

$(
".outer").hover(function () {
clearInterval(ID)
},
function () {
ID
= setInterval(foo, 1000)
})

$(
".right").click(function () {
foo()
})

$(
".left").click(function () {
console.log($(
".img").length)
if (i == 0) {
i
= $(".img").length;
}
i
--;
console.log(i)

$(
".img").eq(i).removeClass("hide").siblings(".img").addClass("hide");
$(
".bottom p").eq(i).addClass("active").siblings().removeClass("active")
})

</script>

</body>
</html>
轮播图