綜合了一下JS與jQuery中一些經常使用的東西,但仍是有好多沒有補充的,今天就先寫這麼一點了,下次再繼續補啦!php
方法 | JavaScript | jQuery |
獲取節點 | document.getElementById();document.getElementByClassName();css document.getElementByTagName();document.getElementByName();html document.querySelector("#Id");document.querySelectorAll(".class");json |
$("#ID");$(".class");瀏覽器 $("TagName");app |
獲取元素內容 | document.getElementById().innerHtml;ide document.getElementById().textContent;函數 |
$("#ID").html();post $("#ID").text();url |
獲取表單值 | document.getElementById().value; | $("input").val(); |
獲取屬性 | document.getElementById().getAttribute("class"); | $("#Id").attr("class") |
刪除屬性 | document.getElementById().removeAttribute("class"); | $("#Id").removeAttr("class") |
設置屬性 | document.getElementById().setAttribute(); | |
設置樣式
獲取樣式
|
document.getElementById().style.height = "";
獲取行內樣式 :ele.style.color;
低版本IE獲取樣式值:ele.currentStyle.color;
其餘瀏覽器獲取樣式值 :window.getComputedStyle(ele,null).color
|
$("#id").css("height","200px")
$("id").css('height')
|
綁定事件 |
var div = document.getElementById();
addEvent(t,"click",function(){});
|
$("#id").on("click",function(){}) |
獲取當前時間 | $.now | new Date().getTime(); |
方法 | JavaScript | jQuery |
文檔就緒函數 | window.onload = function(){} | $(document).ready(function(){}); $(function(){}); |
隱藏顯示 | document.getElementById().style.display = "none/block" |
$("#id").show();
$("#id").hide();
|
建立元素 | createElement('div') | $('<div>') |
移除元素 | removeChild() | .remove() |
克隆元素 | cloneNode(true) | .clone() |
添加元素 | insertBefore()、appendChild() | .insertBefore() 、insertAfter() 、.append() |
替換元素 | replaceChild() | |
json序列化 | JSON.stringify({name:"TG"}) | $.stringify({name:"TG"}) |
json反序列化 | JSON.parse('{"name":"TG"}') | $.parseJSON('{"name":"TG"}') |
方法 | JavaScript | jQuery |
AJAX的GET |
var xhr = new XMLHttpRequest();
xhr.open('GET','handle.php?name=zhangSan',true)
|
$.get("handle.php",{name:"zhangSan"},function(data){}) |
AJAX的POST |
var xhr = new XMLHttpRequest();
xhr.open('POST','handle.php',true);
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr.onreadystatechange = function(){}
xhr.send(null);
|
$.post("handle.php",{name:"zhangSan"},function(data){}) |