반응형
와 간결하네요 줴이쿼리~
http://youmightnotneedjquery.com/ 출처~
AJAX
JSON
JQUERY
$.getJSON('/my/url', function(data) {
});
IE9+
request = new XMLHttpRequest();
request.open('GET', '/my/url', true);
request.onload = function() {
if (request.status >= 200 && request.status < 400){
// Success!
data = JSON.parse(request.responseText);
} else {
// We reached our target server, but it returned an error
}
};
request.onerror = function() {
// There was a connection error of some sort
};
request.send();
Request
JQUERY
$.ajax({
type: 'GET',
url: '/my/url',
success: function(resp) {
},
error: function() {
}
});
IE9+
request = new XMLHttpRequest();
request.open('GET', '/my/url', true);
request.onload = function() {
if (request.status >= 200 && request.status < 400){
// Success!
resp = request.responseText;
} else {
// We reached our target server, but it returned an error
}
};
request.onerror = function() {
// There was a connection error of some sort
};
request.send();
Post
JQUERY
$.ajax({
type: 'POST',
url: '/my/url',
data: data
});
IE8+
var request = new XMLHttpRequest();
request.open('POST', '/my/url', true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
request.send(data);
EFFECTS
ALTERNATIVES:
Fade In
JQUERY
$(el).fadeIn();
IE9+
function fadeIn(el) {
el.style.opacity = 0;
var last = +new Date();
var tick = function() {
el.style.opacity = +el.style.opacity + (new Date() - last) / 400;
last = +new Date();
if (+el.style.opacity < 1) {
(window.requestAnimationFrame && requestAnimationFrame(tick)) || setTimeout(tick, 16)
}
};
tick();
}
fadeIn(el);
ELEMENTS
Add Class
JQUERY
$(el).addClass(className);
IE8+
if (el.classList)
el.classList.add(className);
else
el.className += ' ' + className;
Each
JQUERY
$(selector).each(function(i, el){
});
IE9+
var elements = document.querySelectorAll(selector);
Array.prototype.forEach.call(elements, function(el, i){
});
Filter
JQUERY
$(selector).filter(filterFn);
IE9+
Array.prototype.filter.call(document.querySelectorAll(selector), filterFn);
Finding Elements
JQUERY
$('.my #awesome selector');
IE8+
document.querySelectorAll('.my #awesome selector');
Has Class
JQUERY
$(el).hasClass(className);
IE8+
if (el.classList)
el.classList.contains(className);
else
new RegExp('(^| )' + className + '( |$)', 'gi').test(el.className);
Matches Selector
JQUERY
$(el).is('.my-class');
IE9+
var matches = function(el, selector) {
return (el.matches || el.matchesSelector || el.msMatchesSelector || el.mozMatchesSelector || el.webkitMatchesSelector || el.oMatchesSelector).call(el, selector);
};
matches(el, '.my-class');
Offset
JQUERY
$(el).offset();
IE8+
var rect = el.getBoundingClientRect()
{
top: rect.top + document.body.scrollTop,
left: rect.left + document.body.scrollLeft
}
Outer Height With Margin
JQUERY
$(el).outerHeight(true);
IE9+
function outerHeight(el){
var height = el.offsetHeight;
var style = getComputedStyle(el);
height += parseInt(style.marginTop) + parseInt(style.marginBottom);
return height;
}
outerHeight(el);
Outer Width With Margin
JQUERY
$(el).outerWidth(true);
IE9+
function outerWidth(el){
var height = el.offsetWidth;
var style = getComputedStyle(el);
height += parseInt(style.marginLeft) + parseInt(style.marginRight);
return height;
}
outerWidth(el);
Position Relative To Viewport
JQUERY
var offset = el.offset();
{
top: offset.top - document.body.scrollTop,
left: offset.left - document.body.scrollLeft
}
IE8+
el.getBoundingClientRect()
Remove Class
JQUERY
$(el).removeClass(className);
IE8+
if (el.classList)
el.classList.remove(className);
else
el.className = el.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ');
Set Style
JQUERY
$(el).css('border-width', '20px');
IE8+
// Use a class if possible
el.style.borderWidth = '20px';
Siblings
JQUERY
$(el).siblings();
IE9+
Array.prototype.filter.call(el.parentNode.children, function(child){
return child !== el;
});
Toggle Class
JQUERY
$(el).toggleClass(className);
IE9+
if (el.classList) {
el.classList.toggle(className);
} else {
var classes = el.className.split(' ');
var existingIndex = classes.indexOf(className);
if (existingIndex >= 0)
classes.splice(existingIndex, 1);
else
classes.push(className);
el.className = classes.join(' ');
}
EVENTS
Ready
JQUERY
$(document).ready(function(){
});
IE9+
document.addEventListener('DOMContentLoaded', function(){
});
Trigger Custom
ALTERNATIVES:
JQUERY
$(el).trigger('my-event', {some: 'data'});
IE9+
if (window.CustomEvent) {
var event = new CustomEvent('my-event', {detail: {some: 'data'}});
} else {
var event = document.createEvent('CustomEvent');
event.initCustomEvent('my-event', true, true, {some: 'data'});
}
el.dispatchEvent(event);
Trigger Native
JQUERY
$(el).trigger('change');
IE9+
// For a full list of event types: https://developer.mozilla.org/en-US/docs/Web/API/document.createEvent
event = document.createEvent('HTMLEvents');
event.initEvent('change', true, false);
el.dispatchEvent(event);
UTILS
Deep Extend
JQUERY
$.extend(true, {}, objA, objB);
IE8+
var deepExtend = function(out) {
out = out || {};
for (var i = 1; i < arguments.length; i++) {
var obj = arguments[i];
if (!obj)
continue;
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
if (typeof obj[key] === 'object')
deepExtend(out[key], obj[key]);
else
out[key] = obj[key];
}
}
}
return out;
};
deepExtend({}, objA, objB);
Extend
ALTERNATIVES:
JQUERY
$.extend({}, objA, objB);
IE8+
var extend = function(out) {
out = out || {};
for (var i = 1; i < arguments.length; i++) {
if (!arguments[i])
continue;
for (var key in arguments[i]) {
if (arguments[i].hasOwnProperty(key))
out[key] = arguments[i][key];
}
}
return out;
};
extend({}, objA, objB);
Parse Html
JQUERY
$.parseHTML(htmlString);
IE9+
var parseHTML = function(str) {
var tmp = document.implementation.createHTMLDocument();
tmp.body.innerHTML = str;
return tmp.body.children;
};
parseHTML(htmlString);
Type
JQUERY
$.type(obj);
IE8+
Object.prototype.toString.call(obj).replace(/^\[object (.+)\]$/, "$1").toLowerCase();
반응형
':::: 개발 :::: > ::: JSCRIPT :::' 카테고리의 다른 글
크롬 에서 animate scrollTop 오류 (0) | 2014.11.24 |
---|---|
동적으로 DIV 생성 바디 바로 아래 ~~ (0) | 2014.11.06 |
두번째 탭 선택하기 Jquery (0) | 2014.11.04 |
jquery 페이지 상/하단으로 부드럽게 이동 (0) | 2014.06.19 |
제이쿼리 동작 테스트 해 볼수있는 사이트 (0) | 2014.03.27 |
jquery ready 에서 함수 호출.. (0) | 2014.03.20 |
jqeury radio css control (0) | 2013.11.11 |
햇갈리던 스크립트에 변수 붙이기 ㅠㅠ 아오 (0) | 2013.10.17 |