python學習筆記-jquery
阿新 • • 發佈:2020-08-09
一、什麼是jquery
是輕量級的就是庫(壓縮後只有21k) ,這是其它的js庫所不及的,它相容CSS3,還相容各種瀏覽器
jQuery是一種快速的,簡潔的javaScript庫,使使用者能更方便地處理HTMLdocuments、events、實現動畫效果,並且方便地為網站提供AJAX互動。
另外它的文件說明很全,而且各種應用也說得很詳細,同時還有許多成熟的外掛可供選擇。(https://jquery.cuishifeng.cn/index.html)
二、jquery物件
jQuery物件就是通過jQuery包裝DOM物件後產生的物件。jQuery物件是jQuery獨有的.如果一個物件是jQuery 物件,那麼它就可以使用jQuery裡的方法: $(“#test”).html();
$("#test").html() //意思是指:獲取ID為test的元素內的html程式碼。其中html()是jQuery裡的方法 // 這段程式碼等同於用DOM實現程式碼: document.getElementById(" test ").innerHTML; //雖然jQuery物件是包裝DOM物件後產生的,但是jQuery無法使用DOM物件的任何方法,同理DOM物件也不能使用jQuery裡的方法.亂使用會報錯 //約定:如果獲取的是 jQuery 物件, 那麼要在變數前面加上$.var $variable = jQuery 物件 var variable = DOM 物件 $variable[0]:jquery物件轉為dom物件 $("#msg").html(); $("#msg")[0].innerHTML
jquery的基礎語法:$(selector).action()
*$寫成jQuery也可以,$使用方便。
引入方法:<script src="jquery-3.4.1.min.js"></script>
三、尋找元素
1、選擇器
基本選擇器
$("*") $("#id") $(".class") $("element") $(".class,p,div")
層級選擇器
$(".outer div") $(".outer>div") $(".outer+div") $(".outer~div")
基本篩選器
$("li:first") $("li:eq(2)") $("li:even") $("li:gt(1)")
屬性選擇器
$('[id="div1"]') $('["alex="sb"][id]')
表單選擇器
$("[type='text']")----->$(":text") 注意只適用於input標籤 : $("input:checked")
例子:
<body> <div>hello div</div> <p id="p1" mark="bb">pppp</p> <p id="p3" mark="sb">pppp</p> <a href="">click</a> <p id="p2">ppp</p> <div class="outer"> <div class="inner">inner <p>inner p</p> </div> <p>son</p> outer </div> <p>job</p> <div class="outer2">steven</div> <p>littlepppp</p> <ul> <li>1111</li> <li>2222</li> <li>3333</li> <li>4444</li> <li>5555</li> </ul> <input type="text"> <input type="checkbox"> <input type="submit"> <script src="jquery-3.4.1.min.js"></script> <script> //基本選擇器 $("div").css("color","red"); $("*").css("color","red"); $("#p1").css("color","blue"); $(".outer").css("color","yellow");//拿到多個標籤,會自動遍歷 $(".inner,p,div").css("color","green"); //層級選擇器 $(".outer p").css("color","red");//後臺選擇器 $(".outer>p").css("color","red");//子代選擇器 $(".outer+p").css("color","red")//毗鄰 $(".outer~p").css("color","red")//找兄弟標籤,不一定緊挨著 都是往下找 //基本篩選器 console.log($("ul li")) $("li:first").css("color","red"); $("li:last").css("color","red"); $("li:eq(1)").css("color","red");//篩選出第二個。 $("li:even").css("color","red");//奇數 $("li:odd").css("color","red");//偶數 $("li:gt(1)").css("color","red");//從第二個開始(2,3,4) $("li:lt(2)").css("color","red");//第二個之前(0,1) //屬性選擇器 $("[mark]").css("color","red"); $("[mark='sb']").css("color","red"); $("[mark='sb'][id='p3']").css("color","red");//通過多個屬性查詢 $("[type='text']").css("width","300px"); //表單選擇器 $(":text").css("width","400px");//第二種方式簡寫,只有input表單可以這樣寫 </script> </body>
2、篩選器
過濾篩選器
$("li").eq(2) $("li").first() $("ul li").hasclass("test")
查詢篩選器
$("div").children(".test") $("div").find(".test") $(".test").next() $(".test").nextAll() $(".test").nextUntil() $("div").prev() $("div").prevAll() $("div").prevUntil() $(".test").parent() $(".test").parents() $(".test").parentUntil() $("div").siblings()
例子:
<body> <div>hello div</div> <p id="p1" mark="bb">pppp</p> <p id="p3" mark="sb">pppp</p> <a href="">click</a> <p id="p2">ppp</p> <div class="outer"> <div class="inner">inner <p>inner p</p> </div> <p>son</p> outer </div> <p>job</p> <div class="outer2">steven</div> <p>littlepppp</p> <ul> <li>1111</li> <li>2222</li> <li>3333</li> <li>4444</li> <li>5555</li> <li id="end">6666</li> <li>7777</li> </ul> <input type="text"> <input type="checkbox"> <input type="submit"> <script src="jquery-3.4.1.min.js"></script> <script> //$("li:eq(2)").css("color","red") 之前的寫法 //第二種寫法,推薦 過濾篩選器 $("li").eq(2).css("color","red"); $("li").first().css("color","red"); $("li").last().css("color","red"); //查詢篩選器 $(".outer").children("p").css("color","red");//找子代這一層 $(".outer").find("p").css("color","red");//找後代所有的p $("li").eq(1).next().css("color","red");//下面的一個 $("li").eq(1).nextAll().css("color","red");//下面的所有 $("li").eq(1).nextUntil("#end").css("color","red");//一直找到end為止 $("li").eq(3).prev().css("color","red");//找上面一個 $("li").eq(3).prevAll().css("color","red");//找上面所有 $(".outer .inner p").parent().css("color","red");//找父級 $(".outer .inner p").parents().css("color","red");//一直找父級,直到頂級,很少用這種 $(".outer .inner p").parentsUntil("body").css("color","red");//往上找直到父級是body $(".outer").siblings().css("color","red"); </script> </body>
四、操作元素
1、屬性操作
--------------------------屬性 $("").attr(); $("").removeAttr(); $("").prop(); $("").removeProp(); --------------------------CSS類 $("").addClass(class|fn) $("").removeClass([class|fn]) --------------------------HTML程式碼/文字/值 $("").html([val|fn]) $("").text([val|fn]) $("").val([val|fn|arr]) --------------------------- $("").css("color","red")
例子:
<body> <div class="div1" con="c1"></div> <input type="checkbox" checked="checked">是否可見 <input type="checkbox">是否可見 <input type="text" value="123"> <div value="456"></div> <div id="id1"> uuuu <p>ppp</p> </div> <script src="jquery-3.4.1.min.js"></script> <script> console.log($("div").hasClass("div1")); console.log($("div").attr("con")); console.log($("div").attr("con","c2")); console.log($(":checkbox:first").attr("checked")); //checked console.log($(":checkbox:last").attr("checked"));//attr主要用於找自定義的屬性 //undefined console.log($(":checkbox:first").prop("checked")); //true console.log($(":checkbox:last").prop("checked")); //false console.log($("div").prop("class"));//prop主要用於找自有的屬性 console.log($("#id1").html());//所有內容 如<p>ppp</p> console.log($("#id1").text());//只是文字 ppp console.log($("#id1").html("<h1>hahaha</h1>"));//進行替換 console.log($("#id1").text("<h1>hahaha</h1>")); //固有屬性 console.log($(":text").val()); console.log($(":text").next().val()); $(":text").val("789"); $("div").css({"color":"red","background-color":"green"})
對於HTML元素本身就帶有的固有屬性,在處理時,使用prop方法。 對於HTML元素我們自己自定義的DOM屬性,在處理時,使用attr方法。 像checkbox,radio和select這樣的元素,選中屬性對應“checked”和“selected”,這些也屬於固有屬性,因此 需要使用prop方法去操作才能獲得正確的結果。
jquery迴圈
<body> <p>0000</p> <p>1111</p> <p>2222</p> <script src="jquery-3.4.1.min.js"></script> <script> arr=[11,22,33]; // for (var i=0;i<arr.length;i++){ // $("p").eq(i).html(arr[i]) // } //迴圈遍歷的方式一 $.each(arr,function(x,y){ console.log(x); //0 1 2 console.log(y); //11 22 33 }); //方式二 $("p").each(function(){ console.log($(this)); $(this).html("hello") }) </script> </body>
4.2文件處理
例子:
<body> <div class="c1"> <p>pppp</p> </div> <button>add</button> <script src="jquery-3.4.1.min.js"></script> <script> $("button").click(function(){ // $(".c1").append("<h1>hello</h1>"); var $ele=$("<h1></h1>");//建立一個便籤 $ele.html("hello world"); //內部插入 $(".c1").append($ele);//寫法1 $ele.appendTo(".c1");//寫法2 $(".c1").prepend($ele);//追加到前面 $ele.prependTo(".c1");//寫法2 //外部插入 $(".c1").after($ele);//插入到後面 $ele.insertAfter(".c1");//寫法2 $(".c1").before($ele); $ele.insertBefore(".c1"); //替換 $("p").replaceWith($ele); //刪除與清空 $(".c1").empty(); $(".c1").remove(); //複製 $(".c1").clone() }) </script> </body>
擴充套件:
//內部插入 $("").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");View Code
例項:clone
<body> <div class="outer"> <div class="item"> <button onclick="add(this)">+</button> <input type="text"> </div> </div> <script src="jquery-3.4.1.min.js"></script> <script> function add(self){ var $clone_obj=$(self).parent().clone(); $clone_obj.children("button").html("-").attr("onclick","remove_obj(this)"); $(".outer").append($clone_obj) } function remove_obj(self){ $(self).parent().remove() } </script> </body>
4.3、css操作
CSS $("").css(name|pro|[,val|fn]) 位置 $("").offset([coordinates]) //視口偏移量 $("").position() //已定位父便籤的偏移量 $("").scrollTop([val]) //滾動條,(例項返回頂部) $("").scrollLeft([val]) 尺寸 $("").height([val|fn]) $("").width([val|fn]) $("").innerHeight() $("").innerWidth() $("").outerHeight([soptions]) $("").outerWidth([options])
offset,position
<style> *{ margin:0px; padding:0px; } .div1{ width:200px; height:200px; background-color: red; border:5px solid rebeccapurple; padding: 20px; margin:10px; } .div2{ width:200px; height:200px; background-color: blue; } .outer{ position:relative; } </style> <body> <div class="div1"></div> <div class="outer"> <div class="div2"></div> </div> <script src="jquery-3.4.1.min.js"></script> <script> //offset()相對於視口的偏移量 // console.log($(".div1").offset().top); // console.log($(".div1").offset().left); // // console.log($(".div2").offset().top); // console.log($(".div2").offset().left); //position()相對於已定位的父標籤的偏移量 // console.log($(".div1").position().top); // console.log($(".div1").position().left); // // console.log($(".div2").position().top);//父標籤已定位,這裡就是o,沒定位就是200. // console.log($(".div2").position().left); //css操作 獲取尺寸 console.log($(".div1").height());//只是內容區域的大小, 裡面加引數相當於css定義高度 200 console.log($(".div1").innerHeight());//包含了padding 240 console.log($(".div1").outerHeight());//包含了border 250 console.log($(".div1").outerHeight(true));//包含了margin 270 </script>
五、事件
語法:
事件處理 $("").on(eve,[selector],[data],fn) // 在選擇元素上繫結一個或多個事件的事件處理函式。 // .on的selector引數是篩選出呼叫.on方法的dom元素的指定子元素,如: // $('ul').on('click', 'li', function(){console.log('click');})就是篩選出ul下的li給其繫結 // click事件; [selector]引數的好處: 好處在於.on方法為動態新增的元素也能綁上指定事件;如: //$('ul li').on('click', function(){console.log('click');})的繫結方式和 //$('ul li').bind('click', function(){console.log('click');})一樣;我通過js給ul添加了一個 //li:$('ul').append('<li>js new li<li>');這個新加的li是不會被綁上click事件的 //但是用$('ul').on('click', 'li', function(){console.log('click');}方式繫結,然後動態新增 //li:$('ul').append('<li>js new li<li>');這個新生成的li被綁上了click事件 [data]引數的呼叫: function myHandler(event) { alert(event.data.foo); } $("li").on("click", {foo: "bar"}, myHandler)
例子:
<head> <meta charset="UTF-8"> <title>Title</title> <script src="jquery-3.4.1.min.js"></script> <script> //載入事件 // $(document).ready(function(){ // $("ul li").html(5555) // }); // 簡寫形式 // $(function(){ // $("ul li").html(5555) // }) </script> </head> <body> <ul> <li>11111</li> <li>22222</li> <li>33333</li> <li>44444</li> <li>55555</li> <li>66666</li> </ul> <button>add</button> <script src="jquery-3.4.1.min.js"></script> <script> //繫結事件,寫法1 // $("ul li").click(function(){ // alert(6666) // }); //寫法2 // $("ul li").bind("click",function(){ // alert(6666) // }); //事件委託 //問題,新增加的便籤沒有繫結事件。解決方法是使用事件委託on。 $("ul").on("click","li",function(){ alert(6666) }); // $("ul li").unbind("click");//解除繫結 $("button").click(function(){ var $ele=$("<li>"); var len=$("ul li").length; $ele.html((len+1)*11111); $("ul").append($ele) }) </script> </body>
六、動畫效果
例子:顯示隱藏
<body> <div>hello</div> <button onclick="f1()">顯示</button> <button onclick="f2()">隱藏</button> <button onclick="f3()">切換</button> <script> function f1(){ // $("div").show(); $("div").show(1000);//加引數表示1秒完成動作 } // function f2(){ // // $("div").hide(); // $("div").hide(1000); // } function f3(){ // $("div").toggle(); $("div").toggle(1000);//內部相當於有兩個方法hide和show,當前是hide,就切換成show } //slideDown() 滑動出現 //slideUp() 隱藏 //slideToggle 切換 //fadeIn 淡入效果 //fadeOut 淡出 //fadeToggle 切換 //fadeTo(0.4) 調到設定的值 fadeTo(1000,0.4) //回撥函式 function f2(){ // $("div").hide(); $("div").hide(1000,function(){ alert(11111) }); } </script> </body>
七,擴充套件方法
1、用JQuery寫外掛時,最核心的方兩個方法
語法:
<script> $.extend(object) //為JQuery 新增一個靜態方法。 $.fn.extend(object) //為JQuery例項新增一個方法。 jQuery.extend({ min: function(a, b) { return a < b ? a : b; }, max: function(a, b) { return a > b ? a : b; } }); console.log($.min(3,4)); //----------------------------------------------------------------------- $.fn.extend({ "print":function(){ for (var i=0;i<this.length;i++){ console.log($(this)[i].innerHTML) } } }); $("p").print(); </script>
例子:
<head> <meta charset="UTF-8"> <title>Title</title> <script src="jquery-3.4.1.min.js"></script> </head> <body> <p>pppp</p> <p>0000</p> <p>2222</p> <script> $.extend({ Myprint:function(){ console.log("hello") } }); $.Myprint(); //寫法1 // $.fn.extend({ // GetText:function(){ // for (var i=0;i<this.length;i++) { //寫成$(this)也行 (jquery物件) // console.log(this[i].innerHTML); // } // } // }); //寫法2 $.fn.extend({ GetText:function(){ $.each($(this),function(x,y){ console.log(y.innerHTML) }) } }); $("p").GetText() </script> </body>
八、例項練習
1、正反選
<body> <button onclick="selectAll()">全選</button> <button onclick="cancel()">取消</button> <button onclick="reverse()">反選</button> <hr> <table border="1px"> <tr> <td><input type="checkbox"></td> <td>111</td> <td>111</td> <td>111</td> </tr> <tr> <td><input type="checkbox"></td> <td>222</td> <td>222</td> <td>222</td> </tr> <tr> <td><input type="checkbox"></td> <td>333</td> <td>333</td> <td>333</td> </tr> </table> <script src="jquery-3.4.1.min.js"></script> <script> 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> </body>View Code
2、模態對話方塊
<head> <meta charset="UTF-8"> <title>Title</title> <style> .content{ height: 1800px; background-color: #5cb85c; } .shade{ position: fixed; top:0; left:0; right:0; bottom:0; background-color:whitesmoke; opacity:0.8; } .model{ width:200px; height:200px; background-color:darkslateblue ; position:absolute; top:50%; left:50%; margin-top: -100px; margin-left:-100px; } .hide{ display:none; } </style> </head> <body> <div class="content"> <button onclick="show(this)">show</button> hellohellohellohello </div> <div class="shade hide"></div> <div class="model hide"> <button onclick="cancel(this)">show del</button> </div> <script src="jquery-3.4.1.min.js"></script> <script> function show(self){ $(self).parent().siblings().removeClass("hide"); } function cancel(self){ // $(self).parent().addClass("hide"); // $(self).parent().prev().addClass("hide"); $(self).parent().parent().children(".model,.shade").addClass("hide");//寫法2 } </script> </body>View Code
3、返回頂部
<head> <meta charset="UTF-8"> <title>Title</title> <style> *{ margin:0px; padding:0px; } .div1{ width:100%; height:200px; background-color: red; overflow: auto; } .div2{ width:100%; height:1200px; background-color: blue; } .returnTop{ position:fixed; right:20px; bottom:20px; width:80px; height:50px; background-color: darkgrey; color:white; text-align: center; line-height:50px } .hide{ display: none; } </style> </head> <body> <div class="div1"> <h1>1111</h1> <h1>1111</h1> <h1>1111</h1> <h1>1111</h1> <h1>1111</h1> <h1>1111</h1> <h1>1111</h1> <h1>1111</h1> <h1>1111</h1> <h1>1111</h1> <h1>1111</h1> <h1>1111</h1> </div> <div class="div2"> <button onclick="returnTop2()">return</button> </div> <div class="returnTop hide" onclick="returnTop()">返回頂部</div> <script src="jquery-3.4.1.min.js"></script> <script> window.onscroll=function(){ console.log($(window).scrollTop()); if($(window).scrollTop()>100){ $(".returnTop").removeClass("hide"); }else{ $(".returnTop").addClass("hide"); } }; function returnTop(){ $(window).scrollTop(0); } // function returnTop2(){ // $(".div1").scrollTop(0); // } //事件繫結。返回的寫法2 $(".div2 button").click(function(){ $(".div1").scrollTop(0); }) </script> </body>View Code