寫在前面:javascript
通過系統的學習了原生JS以後,會發現其具備如下三個特色:css
一、是一種解釋性腳本語言(代碼不進行預編譯)。
二、主要用來向 HTML 頁面添加交互行爲。
三、能夠直接嵌入 HTML 頁面,但寫成單獨的js文件有利於結構和行爲的分離。html
而接下來要講的jQuery 就對原生javascript的一個擴展,封裝,就是讓javascript更好用,更簡單。java
換而言之,jquery就是要用更少的代碼,漂亮的完成更多的功能。{Write less, Do more!}jquery
1. jQuery 做爲 JavaScript 的代碼庫,天然是使用 JavaScript 語言編寫的。
2. jQuery 的代碼很是規範,執行效率也很高,是 JavaScript 編碼的優秀範例。
3. 不少情只要使用 jQuery 的方法就能夠實現大部分的 JavaScript 功能。程序員
因此說,程序員做爲一種極懶的物種存在,勢必想着要減小沒必要要的代碼勞動量,所以jQuery誕生了。ajax
1、jQuery基礎語法
json
一、適應JQuery、必需要先導入JQuery。x.x.x.js文件。數組
$(document).ready(function() { //JQuery代碼 }); 簡寫形式以下: $(function(){});
[文檔就緒函數&window.onload的區別]瀏覽器
$("#p").click() √
$("#p").onclick = function(){}; ×
解釋:
$("#p").get(0).onclick() = function () {}; √ $("#p").[0].onclick() = function () {}; √
!function ($) { $()//函數之中,就可使用$代替JQuery對象。 }(jQuery);
② [jQuery.noConflict();]
2、02-JQueryDOM操做及其餘函數
將建立好的節點,插入到指定位置。
在#div1內部的最後,直接插入一個節點。
$("#div1").append("<p>這是被插入的p標籤</p>");
$("<p>這是被插入的p標籤</p>").appendTo("#div1");
$("#div1").prepend("<p>這是被插入的p標籤</p>");
$("#div1").after("<p>這是被插入的p標籤</p>");
$("<p>這是被插入的p標籤</p>").insertBefore("#div1");
$("p").wrap("<div></div>");
$("p").wrapAll("<div></div>");
$("#div1").wrapInner("<div></div>");
刪除元素的父標籤
$("#p").unwrap();
$("p").replaceWith("<span>111</span>");
$("<span>111</span>").replaceAll("p");
$("#div1").empty();
$("#div1").remove();
$("#div1").detach();
二、 不一樣點:
$("#div1").click(function(){ alert(1); }) var div1 = null; $("button:eq(0)").click(function(){ div1 = $("#div1").remove(); }) $("button:eq(1)").click(function(){ div1 = $("#div1").detach(); }) $("button:eq(2)").click(function(){ $("button:eq(2)").after(div1); });
重點 [JS中.cloneNode() 和 JQ中 .clone()區別]
二者都接受傳入true/false的參數。
.cloneNode() 不傳參數或傳入參數false,表示只克隆當前節點,而不克隆子節點。 傳入true表示可隆所有子節點。
$("#div1").clone(true).empty().insertBefore("button:eq(0)")
CSS和屬性相關操做
使用attr()設置或者取到元素的某個屬性。
//$("#div1").attr("class","cls1"); /*$("#div1").attr({ //使用attr一次性設置多個屬性 "class" : "cls1", "name" : "name1", "style" : "color:red;" });*/ console.log($(".p").attr("id")); 刪除元素屬性 $("#div1").removeAttr("class");
console.log($("button:eq(2)").attr("disabled")); console.log($("button:eq(2)").prop("disabled"));
$("p").addClass("selected1 selected2");
$("p").removeClass("selected1");
$("p").toggleClass("selected1");
console.log($("#div1").html());
$("#div1").html("<h1>我是新的h1</h1>");
取到或設置元素裏面的文字內容,至關於innerText
console.log($("#div1").text());
$("#div1").text("<h1>我是新的h1</h1>");
獲取或設置 元素的Value值
console.log($("input[type='text']").val());
$("input[type='text']").val("嘖嘖嘖!");
$("#div1").css({
"color":"red", "user-select":"none", "text-stroke":"0.5px blue" }); var id = setInterval(function(){ $("#div1").css({ "width":function(index,value){ if(parseFloat(value)+10 >= 600){ clearInterval(id); } return parseFloat(value)+10+"px"; } }); },500);
console.log($("#div1").height());
console.log($("#div1").width()); $("#div1").width("400px");
獲取元素的內部寬度。 包括寬高和padding
console.log($("#div1").innerHeight());
console.log($("#div1").innerWidth());
獲取元素的外部寬高。 包括寬高+padding+border
console.log($("#div1").outerHeight(true)); console.log($("#div1").outerWidth());
position():
3、JQuery 事件及動畫
$("button").eq(0).click(function () { alert("快捷綁定!"); })
缺點:綁定的事件沒法取消!
$("button:eq(0)").on ("click",(function () { alert("這是使用on綁定的事件!"); });
$("button:eq(0)").on ("click dbclick",(function () { alert("這是使用on綁定的事件!"); });
$("button:eq(0)").on ({
"click":function () { alert("執行了click事件!") }, "mouseover":function () { alert("執行了mouseover事件!") } });
$("button:eq(0)").on ("click",{name:"wq",age:23},(function (evn) { alert(evn); });
$("document").on("click","p",function(){});
$("p").off("click"):取消單事件綁定
$("p").off("click mouseover dbclick"):取消多事件綁定 $(document).off("click","p"):取消委派事件綁定 $("p").off()取消全部的事件綁定
eg:
$("button").one("click",function () { alert("one作的 只能點一次!") })
4、JQuery 高級 Ajax
$(document).ajaxSend(function(){
alert("ajax請求發送") }); $(document).ajaxStop(function(){ alert("ajax請求中止") }) $(document).ajaxStart(function(){ alert("ajax請求開始") }) $(document).ajaxSuccess(function(){ alert("ajax請求成功") }) $(document).ajaxError(function(){ alert("ajax請求失敗") }) $(document).ajaxComplete(function(){ alert("ajax請求完成(無論成功失敗,都會死乞白賴出來)") })
$("#btn1").click(function(){ var str = $("#form1").serialize(); console.log(str); //str = name=jianghao&password=123&email=1234123 var arr = str.split("&"); console.log(arr); var obj = {}; for (var i=0; i<arr.length; i++) { var arr1 = arr[i].split("="); var keys = arr1[0]; var values = arr1[1]; obj[keys] = values; } console.log(obj); $.get("01-JQuery基礎.html?"+str,obj,function(){ }) })
$("#btn2").click(function(){ var arr = $("#form1").serializeArray(); console.log(arr); var obj = {}; for (var i=0; i<arr.length; i++) { var keys = arr[i].name; var values = arr[i].value; obj[keys] = values; } console.log(obj); });
var str = '{"age":12}'
var obj = $.parseJSON(str); console.log(obj);
var str1 = " 123 "; console.log(str1.trim());
var arr = [1,2,3,4,5,6,7]; var obj = { name : "zhangsan", age : 12, sex : "nan" } $.each(obj,function(index,item){ console.log(index); console.log(item); });
5、JQuery插件 plugin
一、fullpage插件
① afterLoad:當一個頁面加載完成以後觸發
傳遞參數:
anchorLink:當前頁面的錨連接
index:當前頁面的序號,從1開始。
afterLoad:function (anchorLink,index) { console.log(anchorLink); console.log(index); }, //② onLeave:當頁面離開時觸發的函數: /* 接收 index、nextIndex 和 direction 3個參數: index 是離開的「頁面」的序號,從1開始計算; nextIndex 是滾動到的「頁面」的序號,從1開始計算; direction 判斷往上滾動仍是往下滾動,值是 up 或 down */ onLeave:function (index,nextIndex,direction) { console.log(index); console.log(nextIndex); console.log(direction); }, // afterRender 頁面結構生成後的回調函數,或者說頁面初始化完成後的回調函數,執行一次。 // 先執行afterRender 再執行afterLoad: afterRender:function () { console.log("頁面初始化完成了!") }, /* afterSlideLoad:當幻燈片加載完成時執行的函數,接收四個參數 * >>>anchorLink:幻燈片所在的section的錨點 * >>>index:幻燈片所在的section的序號 * >>>slideAnchor:幻燈片自身的錨點(若是沒有顯示slideIdex) * >>>slideIdex:幻燈片自身的序號 */ afterSlideLoad:function (anchorLink,index,slideIndex,direction) { console.log(anchorLink); console.log(index); console.log(slideIndex); console.log(direction); } onSlideLeave 左右移動幻燈片加載以前執行的回調函數,與 onLeave 相似, // 接收 anchorLink、index、slideIndex、direction 4個參數 /* anchorLink:幻燈片所在的section的錨點 index:幻燈片所在的section的序號 slideIndex:當前幻燈片自身的index(從0開始) direction:幻燈片移動的方向left和right nextSlideIndex:即將顯示的幻燈片的index */ onSlideLeave :function function_name () { }
<script type="text/javascript" src="js/move.js"></script>
(move插件並非JQ插件,是原生插件,無需連接jq文件。)
<script type="text/javascript"> document.getElementById('playButton').onclick = function() { move('.box') .set("margin-left","300px") //設置css樣式 .set("margin-top","300px") .add("margin-left", "200px")//add()方法用來增長其已經設置的屬性值。該方法必須數值型值,以便用來增長。 //該方法必須有兩個參數:屬性值和其增量 .sub("margin-left", "200px") //sub()是add()的逆過程,他接受兩個相同的參數,但其值將從屬性值中減去。 .rotate(90)//該方法經過提供的數值做爲參數來旋轉元素。 //當方法被調用的時候經過附加到元素的 transform 屬性上。 .skew(30, 40)//skew()用於調整一個相對於x和y軸的角度。 //該方法能夠被分爲skewX(deg)和skewY(deg)兩個方法 .scale(3, 3) //該方法用於放大或壓縮元素的大小,按照提供的每個值,將調用transform的scale方法 // .then()//用於分割動畫爲兩個集合,並按順序執行。動畫將被分爲兩步,經過then()方法實現分割 .x(300) //設置X軸位置 .y(300) //設置Y軸位置 和添加margin值效果同樣。 .delay(1000) //延時多少毫秒以後執行動畫 .duration('5s')//設置動畫的播放時間。 .end(function() { alert("Animation Over!"); }) //end()該方法用於Move.js代碼片斷的結束,他標識動畫的結束。 //技術上,該方法觸發動畫的播放。該方法接受一個可選的callback回掉函數 }; </script>
三、
$(function () { $("#commentForm").validate({ //rules對象 用於聲明各個input的驗證規則; rules:{ //選擇每一個input時須要先選中該input的name,並對應一個對象設置多條驗證規則; name:{ required:true, minlength:2 }, email:{ required:true, email:true }, url:{ url:true, } }, //messages對象 用於顯示各個input驗證不經過時的提示文字。 //messages對應的各個規則都會有默認的提示,若是沒有特殊須要,無需單獨設置。 messages:{ name:{ required:"這個內容是必填項!", minlength:"這裏最少輸入兩個字符!" }, email:{ required:"這個內容是必填項!", email:"郵箱格式錯誤!" }, url:{ url:"url格式錯誤!", } } }) })