文本操作
$("选择符").html() // 读取指定元素的内容,如果$()函数获取了有多个元素,则提取第一个元素$("选择符").html(内容) // 修改内容,如果$()函数获取了多个元素, 则批量修改内容$("选择符").text() // 效果同上,但是获取的内容是纯文本,不包含html代码$("选择符").text(内容) // 效果同上,但是修改的内容中如果有html文本,在直接转成实体字符,而不是html代码
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <script src="jquery3.6.js"></script> <script> $(function () { $(".c1").click(function () { // 获取文本 // console.log(this.innerHTML); // console.log(this.innerText); console.log($(this).html()); console.log($(this).text()); // 设置文本 // $(this).html("hello world"); // $(this).text("hello world"); // $(this).html("<a href=''>hello world</a>"); $(this).text("<a href=''>hello world</a>"); }) }) </script></head><body><p class="c1"><span>PPP</span></p></body></html
value操作
$().val()
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <script src="jquery3.6.js"></script> <script> $(function () { $("#i1").blur(function () { // 获取jquery对象的value属性值 console.log(this.value); console.log($(this).val()); // 设置value属性值 $(this).val("hello world") }); $("#i3").change(function () { console.log(this.value); console.log($(this).val()); $(this).val("guangdong"); }); console.log($("#i2").val()); console.log($("#i2").val("hello pig!")) }) </script></head><body><input type="text" id="i1"><select id="i3"> <option value="hebei">河北省</option> <option value="hubei">湖北省</option> <option value="guangdong">广东省</option></select><p><textarea name="" id="i2" cols="30" rows="10">123</textarea></p></body></html>
属性操作
//读取属性值 $("选择符").attr("属性名"); // 获取非表单元素的属性值,只会提取第一个元素的属性值 $("选择符").prop("属性名"); // 表单元素的属性,只会提取第一个元素的属性值//操作属性 $("选择符").attr("属性名","属性值"); // 修改非表单元素的属性值,如果元素有多个,则全部修改 $("选择符").prop("属性名","属性值"); // 修改表单元素的属性值,如果元素有多个,则全部修改 $("选择符").attr({'属性名1':'属性值1','属性名2':'属性值2',.....}) $("选择符").prop({'属性名1':'属性值1','属性名2':'属性值2',.....})
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <script src="jquery3.6.js"></script> <script> $(function () { // ================== attr ================== // 获取属性值 // console.log($("#i1").attr("name")); // console.log($("#i1").attr("class")); // 设置属性属性值 // $("#i1").attr("name","mf"); // $("#i1").attr("k1","v1"); // $("#i1").attr({name:"mf","k1":"v1"}); // ================== prop:表单属性 ================== console.log($(":checkbox").attr("checked")); // undefined console.log($(":checkbox").prop("checked")); // false }) </script></head><body><div class="c1" id="i1" name="mufeng">DIV</div><input type="checkbox"></body></html>
<!-- 表格案例 --><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <script src="jquery3.6.js"></script></head><body><button class="select_all">全选</button><button class="cancel">取消</button><button class="reverse">反选</button><hr><table border="1"> <tr> <td>选择</td> <td>姓名</td> <td>年龄</td> </tr> <tr> <td><input type="checkbox"></td> <td>张三</td> <td>23</td> </tr> <tr> <td><input type="checkbox"></td> <td>李四</td> <td>23</td> </tr> <tr> <td><input type="checkbox"></td> <td>王五</td> <td>23</td> </tr></table><script> $(".select_all").click(function () { $("table input:checkbox").prop("checked",true); }); $(".cancel").click(function () { $("table input:checkbox").prop("checked",false); }); $(".reverse").click(function () { $("table input:checkbox").each(function () { $(this).prop("checked",!$(this).prop("checked")) }) });</script></body></html>
<!-- each版 --><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <script src="jquery3.6.js"></script></head><body><button class="select_all">全选</button><button class="cancel">取消</button><button class="reverse">反选</button><hr><table border="1"> <tr> <td>选择</td> <td>姓名</td> <td>年龄</td> </tr> <tr> <td><input type="checkbox"></td> <td>张三</td> <td>23</td> </tr> <tr> <td><input type="checkbox"></td> <td>李四</td> <td>23</td> </tr> <tr> <td><input type="checkbox"></td> <td>王五</td> <td>23</td> </tr></table><script> $(".select_all").click(function () { $("table input:checkbox").prop("checked", true); }); $(".cancel").click(function () { $("table input:checkbox").prop("checked", false); }); $(".reverse").click(function () { /* var $eles = $("table input:checkbox"); for(var i = 0;i<$eles.length;i++){ var state = $($eles[i]).prop("checked"); if (state){ $($eles[i]).prop("checked",false); }else { $($eles[i]).prop("checked",true); } } */ /* $("table input:checkbox").each(function () { var state = $(this).prop("checked"); if (state){ $(this).prop("checked",false); }else { $(this).prop("checked",true); } }) */ $("table input:checkbox").each(function () { $(this).prop("checked", !$(this).prop("checked")) }) });</script></body></html>
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <script src="jquery3.6.js"></script></head><body><ul> <li>23</li> <li>45</li> <li>78</li> <li>32</li></ul><script> // 类方法:each var arr = [111, 222, 333]; $.each(arr, function (i, j) { // 循环体 console.log(i, j) }); var obj = {name: "alvin", age: 18}; $.each(obj, function (k, v) { console.log(k, v); }); // 实例方法each // $("ul li").css("color","red"); $("ul li").each(function () { // console.log("OK"); // 循环函数中this:代指的是每一次循环的dom对象 console.log(this); var v = $(this).html(); if (parseInt(v) > 40) { $(this).css("color", "red") } })</script></body></html>
css样式操作
获取样式$().css("样式属性"); // 获取元素的指定样式属性的值,如果有多个元素,只得到第一个元素的值操作样式$().css("样式属性","样式值").css("样式属性","样式值");$().css({"样式属性1":"样式值1","样式属性2":"样式值2",....})$().css("样式属性":function(){ // 其他代码操作 return "样式值";});
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <script src="jquery3.6.js"></script></head><body><div class="c1">hello JS</div><script> $(".c1").css({"backgroundColor": "#369", "color": "white"})</script></body></html>
class 属性操作
$().addClass("class1 class2 ... ...") // 给获取到的所有元素添加指定class样式$().removeClass() // 给获取到的所有元素删除指定class样式$().toggleClass() // 给获取到的所有元素进行判断,如果拥有指定class样式的则删除,如果没有指定样式则添加
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <script src="jquery3.6.js"></script> <style> .c1 { color: red; } .c2 { background-color: lightseagreen; } .c3 { font-style: italic; } </style></head><body><div class="c1">hello JS</div><script> $(".c1").click(function () { // $(this).addClass("c2"); // $(this).removeClass("c3"); // 链式操作 $(this).addClass("c2").removeClass("c3"); }); $(".c1").mouseover(function () { $(this).addClass("c3"); })</script></body></html>
<!-- 选项卡切换 --><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <style> * { margin: 0; padding: 0; } .tab { width: 800px; height: 300px; /*border: 1px solid red;*/ margin: 200px auto; } .tab ul { list-style: none; } .tab-title { background-color: #f7f7f7; border: 1px solid #eee; border-bottom: 1px solid #e4393c; } .tab .tab-title li { display: inline-block; padding: 10px 25px; font-size: 14px; } li.current { background-color: #e4393c; color: #fff; cursor: default; } .hide { display: none; } </style> <script src="jquery3.6.js"></script></head><body><div class="tab"> <ul class="tab-title"> <li class="current">商品介绍</li> <li class="">规格与包装</li> <li class="">售后保障</li> <li class="">商品评价</li> </ul> <ul class="tab-content"> <li>商品介绍...</li> <li class="hide">规格与包装...</li> <li class="hide">售后保障...</li> <li class="hide">商品评价...</li> </ul></div><script> $(".tab-title li").click(function () { // 事件函数 // 点击标签获取current样式 $(this).addClass("current").siblings().removeClass("current"); // 详情内容的显示与隐藏 console.log($(this).index()); var index = $(this).index(); $(".tab-content li").eq(index).removeClass("hide").siblings().addClass("hide"); })</script></body></html>
节点操作
//创建一个jquery标签对象 $("<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]])
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <script src="jquery3.6.js"></script></head><body><button class="add_btn">添加节点</button><button class="del_btn">删除节点</button><button class="replace_btn">替换节点</button><div class="c1"> <h3>hello JS!</h3> <h3 class="c2">hello world</h3></div><script> $(".add_btn").click(function () { // 创建jquery对象 // var $img = $("<img>"); // $img.attr("src","https://img1.baidu.com/it/u=3210260546,3888404253&fm=26&fmt=auto&gp=0.jpg") // 节点添加 // $(".c1").append($img); // $img.appendTo(".c1") // $(".c1").prepend($img); // $(".c2").before($img); // 支持字符串操作 $(".c1").append("<img src ='https://img1.baidu.com/it/u=3210260546,3888404253&fm=26&fmt=auto&gp=0.jpg'>") }); $(".del_btn").click(function () { $(".c2").remove(); // $(".c2").empty(); }); $(".replace_btn").click(function () { // alert(123); // var $img = $("<img>"); // $img.attr("src","https://img1.baidu.com/it/u=3210260546,3888404253&fm=26&fmt=auto&gp=0.jpg") // $(".c2").replaceWith($img); $(".c2").replaceWith("<img src ='https://img1.baidu.com/it/u=3210260546,3888404253&fm=26&fmt=auto&gp=0.jpg'>"); })</script></body></html>
事件委托
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <script src="jquery3.6.js"></script></head><body><button class="add">add</button><ul> <li>123</li> <li>234</li> <li>345</li></ul><script> /*$("ul li").click(function () { console.log($(this).html()); });*/ $("ul").on("click", "li", function () { console.log($(this).html()) }); $(".add").click(function () { $("ul").append("<li>456</li>") })</script></body></html>
复制节点
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <script src="jquery3.6.js"></script></head><body><div class="outer"> <div class="item"> <input type="button" value="+" class="add_btn"> <input type="text"> </div></div><script> $(".add_btn").click(function () { var $clone = $(this).parent().clone(); $clone.children(".add_btn").val("-").attr("class","remove_btn"); $(".outer").append($clone); }); /*$(".remove_btn").click(function () { $(this).parent().remove(); });*/ $(".outer").on("click",".remove_btn",function () { $(this).parent().remove(); })</script></body></html>
css尺寸
//内容区域$("").height([val|fn]) $("").width([val|fn])//内容区域+padding区域$("").innerHeight()$("").innerWidth()//内容区域+padding+margin$("").outerHeight([soptions])$("").outerWidth([options])
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <style> *{ margin: 0; } .c1{ width: 200px; height: 200px; border:10px solid red; padding: 50px; margin: 50px; } </style> <script src="jquery3.6.js"></script></head><body><div class="c1"></div><script> console.log($(".c1").width()); console.log($(".c1").height()); console.log($(".c1").innerWidth()); console.log($(".c1").innerHeight()); console.log($(".c1").outerWidth(true)); console.log($(".c1").outerHeight(true));</script></body></html>
css位置
$("").offset([coordinates]) // 获取匹配元素在当前视口的相对偏移。$("").position() // 获取匹配元素相对父元素的偏移,position()函数无法用于设置操作。$("").scrollTop([val]) // 获取匹配元素相对滚动条顶部的偏移。
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <style> *{ margin: 0; } .content{ height: 2000px; background-color: lightgray; } .return_top{ width: 120px; height: 50px; background-color: lightseagreen; color: white; text-align: center; line-height: 50px; position: fixed; bottom: 20px; right: 20px; } .hide{ display: none; } </style> <script src="jquery3.6.js"></script></head><body><div class="content"> <h3>文章...</h3></div><div class="return_top hide">返回顶部</div><script> console.log($(window).scrollTop()); $(".return_top").click(function () { $(window).scrollTop(0) }); $(window).scroll(function () { console.log($(this).scrollTop()); var v =$(this).scrollTop(); if (v > 100){ $(".return_top").removeClass("hide"); }else { $(".return_top").addClass("hide"); } })</script></body></html>
<!-- css偏移 --><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <style> .c1{ width: 800px; height: 500px; background-color: lightpink; margin: 100px auto; position: relative; } .c2{ width: 200px; height: 200px; background-color: orange; } </style> <script src="jquery3.6.js"></script></head><body><div class="c1"> <div class="c2"></div></div><script> // 获取位置信息 // offset:相对于窗口偏移 position:相对于已经定位的父标签偏移量 var $offset = $(".c2").offset(); var $position = $(".c2").position(); console.log("$offset top:",$offset.top); console.log("$offset left:",$offset.left); console.log("$position top:",$position.top); console.log("$position left:",$position.left); // 设置偏移量 offset $(".c2").click(function () { $(this).offset({top:500,left:500}) })</script></body></html>
动画效果
//基本 show([s,[e],[fn]]) 显示元素 hide([s,[e],[fn]]) 隐藏元素//滑动 slideDown([s],[e],[fn]) 向下滑动 slideUp([s,[e],[fn]]) 向上滑动//淡入淡出 fadeIn([s],[e],[fn]) 淡入 fadeOut([s],[e],[fn]) 淡出 fadeTo([[s],opacity,[e],[fn]]) 让元素的透明度调整到指定数值//自定义 animate(p,[s],[e],[fn]) 自定义动画 stop([c],[j]) 暂停上一个动画效果,开始当前触发的动画效果
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <style> .c1{ width: 250px; height: 250px; background-color: black; position: absolute; top: 240px; left: 120px; } .hide{ display: none; } </style> <script src="jquery3.6.js"></script></head><body><p> <button class="show01">显示</button> <button class="hide01">隐藏</button></p><p> <button class="show02">显示</button> <button class="hide02">隐藏</button></p><p> <button class="show03">显示</button> <button class="hide03">隐藏</button></p><p> <button class="show04">显示</button> <button class="hide04">隐藏</button></p><p> <button class="animate">animate</button></p><hr><div class="c1"></div><script> // 自己实现的隐藏与显示 $(".show01").click(function () { $(".c1").removeClass("hide") }); $(".hide01").click(function () { $(".c1").addClass("hide") }); // (1) show与hide方法 $(".show02").click(function () { $(".c1").show(1000,function () { alert("显示成功") }); }); $(".hide02").click(function () { $(".c1").hide(1000,function () { alert("隐藏成功") }) }); // (2) slideDown与slideUp $(".show03").click(function () { $(".c1").slideDown(1000,function () { alert("显示成功") }); }); $(".hide03").click(function () { $(".c1").slideUp(1000,function () { alert("隐藏成功") }) }); // (3) fadeIn与fadeOut $(".show04").click(function () { $(".c1").fadeIn(1000,function () { alert("显示成功") }); }); $(".hide04").click(function () { $(".c1").fadeOut(1000,function () { alert("隐藏成功") }) }); // 自定义动画 $(".animate").click(function () { $(".c1").animate({ "border-radius":"50%", "top":340, "left":200 },1000,function () { $(".c1").animate({ "border-radius":"0", "top":240, "left":120 },1000,function () { $(".animate").trigger("click") }) }) })</script></body></html>