1.获取元素
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| $('.xxx'); $('#xxx'); $('.xxx.ccc'); $('.xxx,.zzz'); $('.xxx div'); $('.xxx p:first');
document.querySelector('.xxx'); document.querySelector('#xxx'); document.querySelector('.xxx.ccc'); document.querySelector('.xxx,.ccc'); document.querySelector('.xxx div'); document.querySelector('.xxx p:first-child');
|
2.操作class
1 2 3 4 5 6 7 8 9
| $('.xxx').addClass('class_name'); $('.xxx').removeClass('class_name'); $('.xxx').toggleClass('class_name');
el.classList.add('class_name'); el.classList.remove('class_name'); el.classList.toggle('class_name');
|
3.是否包含某个class
1 2 3 4 5
| $('.xxx').hasClass('class_name');
el.classList.contains('class_name');
|
上面是HTML5提供的新的方法,如果你非要为了兼容所谓的IE,可以用下面的这些
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| function hasClass(o, n){ return new RegExp('\\b'+n+'\\b').test(o.className); };
function addClass(o, n){ if(!hasClass(o, n)) o.className+=' '+n; };
function delClass(o, n){ if(hasClass(o, n)){ o.className = o.className.replace(new RegExp('(?:^|\\s)'+n+'(?=\\s|$)'), '').replace(/^\s*|\s*$/g, ''); }; };
|
4.插入HTML
1 2 3 4 5 6 7 8 9
| $(el).before(htmlString); $(parent).append(el); $(el).after(htmlString);
parent.appendChild(el); el.insertBefore(NewDom,ele); ele.insertAdjacentHTML("beforeend", '<li>内容</li>');
|
5.获取节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| $('.xxx').children(); $('.xxx').prev(); $('.xxx').next(); $('.xxx').parent(); $(ele).siblings();
ele.children; var prev = ele.previousElementSibling || ele.previousSibling; var next = ele.nextElementSibling || ele.nextSibling; ele.parentNode;
var siblings = Array.prototype.slice.call(el.parentNode.children); for (var i = siblings.length; i--;) { if (siblings[i] === el) { siblings.splice(i, 1); break; }; }; ;[].forEach.call(el.parentNode.children, function(child){ if(child !== el); });
|
6.循环节点
1 2 3 4 5 6 7 8 9
| $(selector).each(function(i, el){ });
[].forEach.call(ele,function(el,i){ });
|
7.克隆节点
1 2 3 4 5
| $('.xxx').clone(true);
ele.cloneNode(true);
|
8.操作节点
1 2 3 4 5 6 7
| var ele = $('<div></div>'); $(ele).remove();
var ele = document.createElement('div'); parent.removeChild(ele);
|
9.获取、设置、删除属性
1 2 3 4 5 6 7 8 9
| $(ele).attr(name,value) $(ele).attr(name) $(ele).removeAttr(name)
ele.setAttribute(name,value); ele.getAttribute(name); ele.removeAttribute(name);
|
10.Data属性
1 2 3 4 5 6 7 8 9 10 11 12 13
| $("body").data("foo", 52); $("body").data("foo"); $("body").removeData("foo");
ele.dataset.foo = 52 ele.dataset.foo
ele.setAttribute('data-foo', 52); ele.getAttribute('data-foo'); ele.removeAttribute('data-foo');
|
11.操作内容
1 2 3 4 5 6 7 8 9
| var html = $(ele).html(); $(el).empty(); $(ele).text();
var html = ele.innerHTML; el.innerHTML = ''; var txt = ele.textContent || ele.innerText;
|
12.操作CSS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| $(ele).css('height','300px'); $(ele).css({ height:300 }); $(obj).css('marginLeft');
ele.style.height = '300px'; ele.style.cssText = 'height:200px;color:red;left:100px;' function getStyle(obj,attr){ if(obj.currentStyle){ return obj.currentStyle[attr]; }else{ return getComputedStyle(obj,null)[attr]; }; };
|
13.显示隐藏
1 2 3 4 5 6 7
| $(el).show(); $(el).hide();
el.style.display = ''; el.style.display = 'none';
|
14.元素的高度(宽度同理)[height]
1 2 3 4 5 6 7 8 9
| $(ele).height();
function height(el){ var _height = el.clientHeight; var style = el.currentStyle || getComputedStyle(el); return _height - (parseInt(style.paddingTop) + parseInt(style.paddingBottom)); };
|
元素的内高度(宽度同理)[height + padding]
1 2 3 4 5
| $(ele).innerHeight();
ele.clientHeight;
|
元素的外高度(宽度同理)[height + padding + border]
1 2 3 4 5
| $(ele).outerHeight();
ele.offsetWidth;
|
元素的外高度(宽度同理)[height + padding + border + margin]
1 2 3 4 5 6 7 8 9
| $(ele).outerHeight(true);
function outerHeight(el){ var style = el.currentStyle || getComputedStyle(el); var height = el.offsetHeight + parseInt(style.marginTop) + parseInt(style.marginBottom); return height; };
|
15.元素的位置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| $(ele).position().left; $(ele).position().top; $(ele).offset().left; $(ele).offset().top;
ele.offsetLeft; ele.offsetTop; function getposi(obj){ var t = 0,l = 0; while(obj){ t+=obj.offsetTop; l+=obj.offsetLeft; obj = obj.offsetParent; }; return {top:t,left:l}; };
ele.getBoundingClientRect().top + window.pageYOffset; ele.getBoundingClientRect().bottom + window.pageYOffset; ele.getBoundingClientRect().left + window.pageYOffset; ele.getBoundingClientRect().right + window.pageXOffset;
|
16.Document事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| $(document).ready(function() { });
$(window).load(function() { });
$(document).click(function(){ });
$(ele).on('click',function(){ });
document.addEventListener("DOMContentLoaded", function() { },false);
document.addEventListener("load", function() { },false);
document.onclick = function(){ };
document.addEventListener("click", function() { },false); [].forEach.call(ele,function(o){ o.addEventListener("click", function() { },false); });
|
17.获取数据类型
1 2 3 4 5 6 7
| $.type(obj);
function type(obj){ return Object.prototype.toString.call(obj).replace(/^\[object (.+)\]$/, "$1").toLowerCase(); };
|
18.判断是否为数组
1 2 3 4 5 6 7
| $.isArray(arr);
function isArray (v){ return Object.prototype.toString.call(v) === '[object Array]'; };
|
19.去除字符串两端的空格
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| $.trim(string);
String.prototype.trim = function() { var reExtraSpace = /^\s*(.*?)\s+$/; return this.replace(reExtraSpace, "$1") }
String.prototype.ltrim = function() { return this.replace( /^(\s*| *)/, ""); }
String.prototype.rtrim = function() { return this.replace( /(\s*| *)$/, ""); }
String.prototype.replaceAll = function(s1, s2) { return this.replace(new RegExp(s1, "gm"), s2) }
String.prototype.trimAll = function() { var reExtraSpace = /\s*(.*?)\s+/; return this.replaceAll(reExtraSpace, "$1") }
|
20.Cookie
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| $.cookie('cookie'); $.cookie('cookie', 'value'); $.cookie('cookie', 'value', { expires: 7 }); $.cookie('cookie', '', { expires: -1 });
function setCookie(){ var json = {},d; if(typeof arguments[0] ==’string’){ json[arguments[0]] = arguments[1]; d = arguments[2] }else{ for(var i in arguments[0]){ json.i = arguments[0][i] d = arguments[1]; }; }; var t = new Date(); t.setDate(t.getDate()+d); for(var j in json){ document.cookie = j+’=’+json[j]+';expires=’+t; }; };
function getCookie(n){ var a = document.cookie.split(‘; ‘); for(var i=0;i<a.length;i++){ var a2 = a[i].split(‘=’); if(a2[0]==n){ return a2[1]; }; }; };
function removeCookie(n){ setCookie(n,null,-1); }
|
21.Ajax
1 2 3 4 5 6 7 8 9 10 11
| $.ajax({ type: 'POST', url: '/my/url', data: data });
var request = new XMLHttpRequest(); request.open('POST', '/my/url', true); request.send(data);
|