标签:text slide scrolltop var 鼠标移动 xtu play 滚动条 padding
jQuery是一个快速的,简洁的javaScript库,使用户能更方便地处理HTMLdocuments、events、实现动画效果,并且方便地为网站提供AJAX交互。
$("#msg").html(); $("#msg")[0].innerHTML1、基本选择器 $("*") $("#id") $(".class") $("element") $(".class,p,div") 2、层级选择器 $(".outer div") $(".outer>div") $(".outer+div") $(".outer~div") 3、属性选择器 $(‘[id="div1"]‘) $("[name!=‘newsletter‘]") $("[name^=‘news‘]") $("input[name*=‘man‘]") $("input[id][name$=‘man‘]")//且的关系 4、表单选择器 $("[type=‘text‘]") = $("input:text") = $(":text") //注意只适用于input标签 5、表单对象属性 $("input:enabled") $("input:disabled") $("input:checked") $("select option:selected")=$("option:selected") 1、基本筛选器 $("li:first") $("li:eq(2)") $("li:even") $("li:gt(1)") 2、过滤筛选器 $("li").eq(2) $("li").first() $("ul li").hasClass("test") $("p").filter(".selected, :first") //或的关系 3、查找筛选器 查找子标签: $("div").children(".test") $("div").find(".test") 向下查找兄弟标签: $(".test").next() $(".test").nextAll() $(".test").nextUntil() 向上查找兄弟标签: $("div").prev() $("div").prevAll() $("div").prevUntil() 查找所有兄弟标签: $("div").siblings() 查找父标签: $(".test").parent() $(".test").parents() $(".test").parentsUntil() 4、返回(选取所有的p元素,查找并选取span子元素,然后再回过来选取p元素) $("p").find("span").end()
1、页面载入 $(document).ready(function(){}) = $(function(){}) 2、事件绑定 //语法:标签对象.事件(函数) $("p").click(function(){}) 3、事件委派 $("").on(eve,[selector],[data],fn) // 在选择元素上绑定一个或多个事件的事件处理函数。 $("ul").on("click","li",func) $("ul").off("click","li",func) $("p").on("click", function(){ alert( $(this).text());}); 4、事件切换 $(".test").hover(enter,out) hover事件: 一个模仿悬停事件(鼠标移动到一个对象上面及移出这个对象)的方法。这是一个自定义的方法,它为频繁使用的任务提供了一种“保持在其中”的状态。 over:鼠标移到元素上要触发的函数 out:鼠标移出元素要触发的函数
<!DOCTYPE html> <html lang="en" style="padding: 0px"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <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="https://code.jquery.com/jquery-3.1.1.min.js"></script> <script> /* $("ul li").click(function(){ alert(123) }); $("#off").click(function(){ $("ul li").off() }) */ $("#add_li").click(function(){ var $ele=$("<li>"); $ele.text(Math.round(Math.random()*10)); $("ul").append($ele) }); function test(){alert(222)} $("ul").on("click","li",test) $("#off").click(function(){ $("ul").off("click","li",test) }) </script> </body> </html>
<!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="https://code.jquery.com/jquery-3.1.1.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>
1、CSS类 $("").addClass(class|fn) $("").removeClass([class|fn]) 2、属性 $("").attr(); //对于HTML元素的自定义DOM属性,在处理时,使用attr方法。 $("").removeAttr(); $("").prop(); //对于HTML元素的固有属性,在处理时,使用prop方法。 $("").removeProp(); 注意: //像checkbox,radio和select这样的元素,选中属性对应"checked"和"selected",这些也属于固有属性 //因此需要使用prop方法去操作才能获得正确的结果。 3、HTML代码/文本/值 $("").html([val|fn]) $("").text([val|fn]) $("").val([val|fn|arr]) 4、CSS样式 $("#c1").css({"color":"red","fontSize":"35px"}) $("p").css("color","red")
1、创建一个标签对象 $("<p>") 2、内部插入 $("").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"); 3、外部插入 $("").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"); 4、替换 $("").replaceWith(content|fn) ----->$("p").replaceWith("<b>Paragraph. </b>"); 5、删除 $("").empty() $("").remove([expr]) 6、复制 $("").clone([Even[,deepEven]])
位置操作 $("").offset([coordinates]) //相对当前视口的偏移,只对可见元素有效。 $("").position() //相对父元素的偏移,只对可见元素有效。 $("").scrollTop([val]) //相对滚动条的偏移,此方法对可见和隐藏元素均有效。 $("").scrollLeft([val]) 尺寸操作 $("").height([val|fn]) $("").width([val|fn]) $("").innerHeight() //获取内部区域高度(包括补白、不包括边框),此方法对可见和隐藏元素均有效。 $("").innerWidth() $("").outerHeight([soptions]) //获取外部高度(默认包括补白和边框),设置为true时,计算边距在内,此方法对可见和隐藏元素均有效。 $("").outerWidth([options])
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body{
margin: 0;
}
.returnTop{
height: 60px;
width: 100px;
background-color: peru;
position: fixed;
right: 0;
bottom: 0;
color: white;
line-height: 60px;
text-align: center;
}
.div1{
background-color: wheat;
font-size: 5px;
overflow: auto;
width: 500px;
height: 200px;
}
.div2{
background-color: darkgrey;
height: 2400px;
}
.hide{
display: none;
}
</style>
</head>
<body>
<div class="div1 div">
<h1>hello</h1>
<h1>hello</h1>
<h1>hello</h1>
<h1>hello</h1>
<h1>hello</h1>
<h1>hello</h1>
<h1>hello</h1>
<h1>hello</h1>
<h1>hello</h1>
<h1>hello</h1>
<h1>hello</h1>
<h1>hello</h1>
<h1>hello</h1>
<h1>hello</h1>
<h1>hello</h1>
<h1>hello</h1>
</div>
<div class="div2 div"></div>
<div class="returnTop hide">返回顶部</div>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>
$(window).scroll(function(){
var current=$(window).scrollTop();
if (current>100){
$(".returnTop").removeClass("hide")
}
else {
$(".returnTop").addClass("hide")
}
});
$(".returnTop").click(function(){
$(window).scrollTop(0)
});
</script>
</body>
</html>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script><script>li=[10,20,30,40];dic={name:"alex",sex:"male"};list=[{name:"alex",sex:"male"},{name:"alex22",sex:"female"}];$.each(li,function(i,x){ console.log(i,x)});$.each(dic,function(i,x){ console.log(i,x)});$.each(list,function(i,x){ console.log(i,x)});/*0 101 202 303 40name alexsex male0 {name: "alex", sex: "male"}1 {name: "alex22", sex: "female"}*/</script> |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<body><p>111</p><p>222</p><p>333</p></body><script src="https://code.jquery.com/jquery-3.1.1.min.js"></script><script>$("p").each(function(){ console.log($(this).html()) //$(this)代指当前循环标签。})/*111222333*/</script> |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script><script>function f(){ for(var i=0;i<4;i++){ if (i==2){ return } console.log(i) // 1,2 } } f(); $.each($("p"),function(i,v){ if (i==2){ return false; } console.log(v) //A B }); $.each($("p"),function(i,v){ if (i==2){ return true; } console.log(v) //A B D }); li=[11,22,33,44]; $.each(li,function(i,v){ if (v==33){ return ; } console.log(v) //11,22,44 }); $.each(li,function(i,v){ if (v==33){ return false; } console.log(v) //11,22 }); // <1>如果你想return后下面循环函数继续执行,那么就直接写return或return true // <2>如果你不想return后下面循环函数继续执行,那么就直接写return false</script> |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
<script>//显隐$(document).ready(function() { $("#hide").click(function () { $("p").hide(1000); }); $("#show").click(function () { $("p").show(1000); });//用于切换被选元素的 hide() 与 show() 方法。 $("#toggle").click(function () { $("p").toggle(); });})//滑动$(document).ready(function(){ $("#slideDown").click(function(){ $("#content").slideDown(1000); }); $("#slideUp").click(function(){ $("#content").slideUp(1000); }); $("#slideToggle").click(function(){ $("#content").slideToggle(1000); })});//淡入淡出$(document).ready(function(){ $("#in").click(function(){ $("#id1").fadeIn(1000); }); $("#out").click(function(){ $("#id1").fadeOut(1000); }); $("#toggle").click(function(){ $("#id1").fadeToggle(1000); }); $("#fadeto").click(function(){ $("#id1").fadeTo(1000,0.4); });});</script> |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Title</title></head><body> <button>fadeToggle</button> <p>helloworld helloworld helloworld</p><script src="https://code.jquery.com/jquery-3.1.1.min.js"></script><script> $("button").click(function(){ $("p").fadeToggle(1000,function(){ console.log($(this).html()) }) })</script></body></html> |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body><input type="checkbox"><input type="checkbox"><input type="checkbox"></body><script src="https://code.jquery.com/jquery-3.1.1.min.js"></script><script> //扩展jQuery对象本身 jQuery.extend({ min: function(a, b) { return a < b ? a : b; }, max: function(a, b) { return a > b ? a : b; }}); console.log(jQuery.min(2,3)); // => 2 console.log(jQuery.max(4,5)); // => 5 //元素 jQuery.fn.extend({ check: function() { $(this).attr("checked",true); }, uncheck: function() { $(this).attr("checked",false); } }); $(":checkbox:gt(0)").check()</script></html> |
标签:text slide scrolltop var 鼠标移动 xtu play 滚动条 padding
原文地址:https://www.cnblogs.com/bubu99/p/10463843.html