使用JQuery首先要在 HTML 代碼最前面加上對 jQuery 庫的引用,好比:css
<script src="js/jquery.min.js"></script>
庫文件既能夠放在本地,也能夠直接使用知名公司的 CDN,好處是這些大公司的 CDN 比較流行,用戶訪問你網站以前極可能在訪問別的網站時已經緩存在瀏覽器中了,因此能加快網站的打開速度。另一個好處是顯而易見的,節省了網站的流量帶寬。例如:html
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script> //Google
或者:
<script src="http://code.jquery.com/jquery-1.6.min.js"></script> //jQuery 官方
a.JavaScript使用jquery
getElement系列、query系列ajax
<body> <ul> <li id="first">哈哈</li> <li class="cls" name ="na">啦啦</li> <li class="cls">呵呵</li> <li name ="na">嘿嘿</li> </ul> <div id="div"> <ul> <li class="cls">呵呵</li> <li>嘿嘿</li> </ul> </div> </body> <script> document.getElementById("first"); //是一個元素 document.getElementsByClassName("cls"); //是一個數組,即便只有一個元素,使用時須要用[0]取到第一個再使用 document.getElementsByName("na"); //是一個數組,即便只有一個元素,使用時須要用[0]取到第一個再使用
document.getElementsByTagName("li"); //是一個數組,即便只有一個元素,使用時須要用[0]取到第一個再使用
document.querySelector("#div"); //是一個元素
document.querySelectorAll("#div li"); //是一個數組,即便只有一個元素,使用時須要用[0]取到第一個再使用
</script>
b.JQuery使用編程
大量的選擇器同時使用$()包裹選擇器api
<body> <ul> <li id="first">哈哈</li> <li class="cls" name ="na">啦啦</li> <li class="cls">呵呵</li> <li name ="na">嘿嘿</li> </ul> <div id="div"> <ul> <li class="cls">呵呵</li> <li>嘿嘿</li> </ul> </div> </body> <script src="http://code.jquery.com/jquery-1.6.min.js"></script> <script>
//使用JQuery取到的是jquery對象都是一個數組,即便只有一個元素被選中,可是在使用時候不必定須要使用:eq(0)來拿到這一個在使用能夠直接使用 $("#first"); $(".cls"); $("li type[name='na']"); $("li"); $("#div"); $("#div li"); </script>
a.JavaScript使用 數組
getAttribute("屬性名") 、 setAttribute("屬性名","屬性值")瀏覽器
<body> <ul> <li id="first">哈哈</li> </ul> </body> <script> document.getElementById("first").getAttribute("id"); document.getElementById("first").setAttribute("name","nafirst");
document.getElementById("first").removeAttribute("name");
</script>
b.JQuery使用緩存
.attr()傳入一個參數獲取,傳入兩個參數設置框架
.prop()
prop和attr同樣均可以對文本的屬性進行讀取和設置;
二者的不一樣 在讀取checked,disabled,等屬性名=屬性值的屬性時
attr返回屬性值或者undefined,當讀取的checked屬性時不會根據是否選中而改變
prop返回true和false 當讀取的checked屬性時會根據是否選中而改變
也就是說attr要取到的屬性必須是在標籤上寫明的屬性,不然不能取到
<body> <ul> <li id="first">哈哈</li> </ul> </body>
<script src="js/jquery.js"></script> <script> $("#first").attr("id"); $("#first").attr("name","nafirst");
$("#first").removeAttr("name");
$("#first").prop("id");
$("#first").prop("name","nafirst");
$("#first").removeProp("name");
</script>
a.JavaScript使用
innerHTML:取到或設置一個節點的HTML代碼,能夠取到css,以文本的形式返回
innerText:取到或設置一個節點的HTML代碼,不能取到css
value:取到input[type='text']輸入的文本
<body> <ul> <li id="serven_times" ><span style="color: chartreuse">嘿嘿</span></li> <li id="eight_times" ><span style="color: chartreuse">嘿嘿</span> </li> </ul> 姓名:<input type="text" id="input"> </body> <script> document.getElementById("serven_times").innerHTML; document.getElementById("serven_times").innerHTML = "<span style='color: #ff3a29'>呵呵</span>"; document.getElementById("eight_times").innerText; document.getElementById("eight_times").innerText = "啦啦"; document.getElementById("input").value; </script>
b.JQuery使用
.html()取到或設置節點中的html代碼
.text()取到或設置節點中的文本
.val()取到或設置input的value屬性值
<body> <ul> <li id="serven_times" ><span style="color: chartreuse">嘿嘿</span></li> <li id="eight_times" ><span style="color: chartreuse">嘿嘿</span> </li> </ul> 姓名:<input type="text" id="input"> </body> <script src="/js/jquery.min.js"></script> <script> $("#serven_times").html(); $("#serven_times").html("<span style='color: #ff3a29'>呵呵</span>"); $("#eight_times").text(); $("#eight_times").text("啦啦"); $("#input").val(); $("#input").val("哈哈"); </script>
JavaScript:
* 1.使用setAttribute設置class和style
* document.getElementById("first").setAttribute("style","color:red");
* 2.使用.className添加一個class選擇器
* document.getElementById("third").className = "san";
* 3.使用.style.樣式直接修改單個樣式。注意樣式名必須使用駝峯命名法
* document.getElementById("four_times").style.fontWeight = "900";
* 4.使用.style或.style.cssText添加一串行級樣式:
* document.getElementById("five_times").style = "color: blue;";//IE不兼容
* document.getElementById("six_times").style.cssText = "color: yellow;font-size : 60px;";
JQuery:
$("#div2").css("color","yellow");
$("#div2").css({
"color" : "white",
"font-weight" : "bold",
"font-size" : "50px",
});
JavaScript:
*1.childNodes:獲取當前節點的全部子節點(包括元素節點和文本節點)
* children:獲取當前節點的全部元素子節點(不包括文本節點)
*2.parentNode:獲取當前節點的父節點
*3.firstChild:獲取第一個元素節點,包括回車等文本節點
* firstElementChild:獲取第一個元素節點,不包括回車節點
* lastChild、lastElementChild 同理
*4.previousSibling:獲取當前元素的前一個兄弟節點
* previousElementSibling::獲取當前元素的前一個兄弟節點
* nextSibling、nextElementSibling
JQuery:
1.提供了大量的選擇器:
1.9+
2.除此以外也提供了對應的函數:
first() last() children() parents() parent() siblings()
JavaScript:
使用了Dom0事件模型和Dom2事件模型,具體內容見我上一篇博客
JQuery:
①.事件綁定的快捷方式
<body> <button>按鈕</button> </body> <script src="js/jquery-1.10.2.js"></script> <script> $("button:eq(0)").click(function () { alert(123); }); </script>
②:使用on進行事件綁定
<body> <button>按鈕</button> </body> <script src="js/jquery-1.10.2.js"></script> <script> //①使用on進行單事件的綁定 $("button:eq(0)").on("click",function () { alert(456); }); //②使用on同時給同一對象綁定多個事件 $("button:eq(0)").on("click dblclick mouseover",function () { console.log(123); }); //③使用on,給一個對象綁定多個事件 $("button:eq(0)").on({ "click":function () { console.log("click"); }, "mouseover":function () { console.log("mouseover"); }, "mouseover":function () { console.log("mouseover2"); } }); //④使用on給回調函數傳參,要求是對象格式,傳遞的參數能夠在e.data中取到;jquery中的e只能經過參數傳進去,不能用window.event $("button:eq(0)").on("click",{"name":"zhangsan","age":15},function (e) { console.log(e); console.log(e.data); console.log(e.data.name); console.log(e.data.age); console.log(window.event);//js中的事件因子 }); </script>
*①.window.onload必須等待網頁資源(包括圖片等)所有加載完成後,才能執行;
* 而文檔就緒函數只須要等到網頁DOM結構加載完成後,便可執行
*②.window.onload在一個頁面中,只能寫一次,寫屢次會被最後一次覆蓋
* 而文檔就緒函數在一個頁面中能夠有N個
① 使用$("")取到的節點爲JQuery對象,只能調用JQuery方法,不能調用JavaScript方法;
* $("#div").click(function(){})√
* $("#div").onclick = function(){}× 使用JQuery對象調用JavaScript方法
*
* 同理,使用、document.getElement系列函數取到的對象爲JavaScript對象,也不能調用JQery函數
*① JQuery ---> JavaScript :使用get(index)或者[index]選中的就是JavaScript對象
* $("div").get(0).onclick = function(){}
* $("div").[0].onclick = function(){}
* ② JavaScript ---> JQuery :使用$()包裹JavaScript對象 (咱們發現JQuery無論得到幾個對象都是一個數組,能夠直接給整個數組都添加某一事件)
* var div = document.getElementById("div");
* $(div).click(function(){});
聲明:此博客爲博主原創如需轉載請註明初出:http://www.cnblogs.com/ytsbk/p/9033837.html