不要过度依赖JQuery(一)
$('#test') => document.getElementById('test');
$('.test') => document.getElementsByClassName('test')
$('div') => document.getElementsByTagName('div')
$('#test')
document.querySelector('#test')
$('#test div') => document.querySelectorAll('#test div')
$('#test').find('span') => document.querySelectorAll('#test span');
$('#test div').eq(0)[0] => document.querySelectorAll('#test div')[0]
$('html') => document.documentElement
$('head') => document.head
$('body') => document.body
$('#test').length > 0 => document.getElementById('test') !== null
$('div').length > 0 => document.querySelectorAll('div').length > 0
$('div').each(function(i, elem) {})
function forEach(elems, callback) {
if([].forEach) {
[].forEach.call(elems, callback);
} else {
for(var i = 0; i < elems.length; i++) {
callback(elems[i], i);
}
}
}
var div = document.querySelectorAll('div');
forEach(div, function(elem, i){
});
$('#test').empty() => document.getElementById('test').innerHTML = '';
$('div').is($('#test')) => document.querySelector('div') === document.getElementById('test')
$('div').html() => document.querySelecotr('div').innerHTML
$('div').text() => var t = document.querySelector('div');
t.textContent || t.innerText;
$('div').html('<span>abc</span>'); => document.querySelecotr('div').innerHTML = '<span>abc</span>';
$('div;).text('abc') => document.querySelecotr('div').textContent = 'abc'
$('<div>').append($('#test').clone()).html() => document.getElementById('test').outerHTML
$('<div>').append($('#test').clone()).html('<span>abc</span>') => document.getElementById('test').outerHTML = '<span>abc</span>'
$('input').val() => document.querySelector('input').value
$('input').val('abc') => document.querySelector('input').value = 'abc'
$('#test').addClass('a')
function addClass(elem, className) {
if(elem.classList) {
elem.classList.add(className);
} else {
elem.className += ' ' + className;
}
}
addClass(document.getElementById('test'), 'a');
$('#test').removeClass('a');
function removeClass(elem, className) {
if(elem.classList) {
elem.classList.remove(className);
} else {
elem.className = elem.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ');
}
}
removeClass(document.getElementById('test'), 'a');
$('#test').hasClass('a')
function hasClass(elem, className) {
if(elem.classList) {
return elem.classList.contains(className);
} else {
return new RegExp('(^| )' + className + '( |$)', 'gi').test(elem.className);
}
}
hasClass(document.getElementById('test'), 'a');
$('<div>') => document.createElement('div')
$('div').clone() => document.querySelector('div').cloneNode(true)
$('div').append('<span></span>')
var span = document.createElement('span');
document.querySelector('div').appendChild(span);
$('<span>').insertBefore('#test');
var t = document.getElementById('test');
var span = document.createElement('span');
t.parentNode.insertBefore(span, t);
/*更简单的*/
t.insertAdjacentHTML('beforeBegin', '<span></span>');
$('<span>').insertAfter('#test')
function insertAfter(elem, newNode){
if(elem.nextElementSibling) {
elem.parentNode.insertBefore(newNode, elem.nextElementSibling);
} else {
elem.parentNode.appendChild(newNode);
}
}
var t = document.getElementById('test');
var span = document.createElement('span');
insertAfter(t, span);
/*更简单的*/
t.insertAdjacentHTML('afterEnd', '<span></span>');
$('#test').parent() => document.getElementById('test').parentNode
$('#test').remove()
var t = document.getElementById('test');
t.parentNode.removeChild(t);
获取Element子节点
$('#test').children()
function children(elem) {
if(elem.children) {
return elem.children;
} else {
var children = [];
for (var i = el.children.length; i--;) {
if (el.children[i].nodeType != 8)
children.unshift(el.children[i]);
}
return children;
}
}
children(document.getElementById('test'));
获取下一个兄弟节点:
$('#test').next()
function nextElementSibling(elem) {
if(elem.nextElementSibling) {
return elem.nextElementSibling;
} else {
do {
elem = elem.nextSibling;
} while ( elem && elem.nodeType !== 1 );
return elem;
}
}
nextElementSibling(document.getElementById('test'));
获取上一个兄弟节点:
$('#test').prev()
function previousElementSibling(elem) {
if(elem.previousElementSibling) {
return elem.previousElementSibling;
} else {
do {
elem = elem.previousSibling;
} while ( elem && elem.nodeType !== 1 );
return elem;
}
}
previousElementSibling(document.getElementById('test'));
5、属性操作
获取属性
$('#test').attr('class') => document.getElementById('test').getAttribute('class')
删除属性
$('#test').removeAttr('class') => document.getElementById('test').removeAttribute('class')
设置属性
$('#test').attr('class', 'abc') => document.getElementById('test').setAttribute('class', 'abc');
6、CSS样式操作
设置样式
$('#test').css('height', '10px'); => document.getElementById('test').style.height = '10px';
获取样式
$('#test').css('height')
var getStyle = function(dom, attr) {
return dom.currentStyle ? dom.currentStyle[attr] : getComputedStyle(dom, false)[attr];
};
getStyle(document.getElementById('test'), 'height');
获取伪类的CSS样式
window.getComputedStyle(el , ":after")[attrName];
注:IE是不支持获取伪类的
7、字符串操作
去除空格
$.trim(' abc ')
function trim(str){
if(str.trim) {
return str.trim();
} else {
return str.replace(/^\s+|\s+$/g, '');
}
}
trim(' abc ');
8、JSON操作
JSON序列化
$.stringify({name: "TG"}) => JSON.stringify({name: "TG"})
JSON反序列化
$.parseJSON('{ "name": "TG" }') => JSON.parse('{ "name": "TG" }')
如有错误,欢迎指正!如有更好建议,欢迎留言!