js与jQuery经常使用方法

综合了一下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){})
相关文章
相关标签/搜索