You might not need jQuery

You certainly can support IE 9 and below without jQuery, but we don't. Please stop messing with us.

jQuery and its cousins are great, and by all means use them if it makes it easier to develop your application.

If you're developing a library on the other hand, please take a moment to consider if you actually need jQuery as a dependency. Maybe you can include a few lines of utility code, and forgo the requirement. If you're only targeting more modern browsers, you might not need anything more than what the browser ships with.

At the very least, make sure you know what jQuery is doing for you, and what it's not. Some developers believe that jQuery is protecting us from a great demon of browser incompatibility when, in truth, post-IE8, browsers are pretty easy to deal with on their own.

What's the oldest version of IE you need to support?

8
9
10
Your search didn't match any comparisons.

AJAX

Alternatives:

Post

jQuery

$.ajax({
type: 'POST',
url: '/my/url',
data: data
});

IE8+

var request = new XMLHttpRequest();
request.open('POST', '/my/url', true);
request.send(data);

JSON

jQuery

$.getJSON('/my/url', function(data) {

});

IE8+

request = new XMLHttpRequest;
request.open('GET', '/my/url', true); request.onreadystatechange = function() {
if (this.readyState === 4){
if (this.status >= 200 && this.status < 400){
// Success!
data = JSON.parse(this.responseText);
} else {
// Error :(
}
} request.send();
request = null;

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();

IE10+

request = new XMLHttpRequest
request.open('GET', '/my/url', true) request.onload = function() {
if (this.status >= 200 && this.status < 400){
// Success!
data = JSON.parse(this.response)
} 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() { }
});

IE8+

request = new XMLHttpRequest;
request.open('GET', '/my/url', true); request.onreadystatechange = function() {
if (this.readyState === 4){
if (this.status >= 200 && this.status < 400){
// Success!
resp = this.responseText;
} else {
// Error :(
}
}
} request.send();
request = null;

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()

IE10+

request = new XMLHttpRequest
request.open('GET', '/my/url', true) request.onload = function() {
if (this.status >= 200 && this.status < 400){
// Success!
resp = this.response
} else {
// We reached our target server, but it returned an error }
} request.onerror = function() {
// There was a connection error of some sort
} request.send()

Effects

Fade In

jQuery

$(el).fadeIn();

IE8+

function fadeIn(el) {
var opacity = 0; el.style.opacity = 0;
el.style.filter = ''; var last = +new Date();
var tick = function() {
opacity += (new Date() - last) / 400;
el.style.opacity = opacity;
el.style.filter = 'alpha(opacity=' + (100 * opacity)|0 + ')'; last = +new Date(); if (opacity < 1) {
(window.requestAnimationFrame && requestAnimationFrame(tick)) || setTimeout(tick, 16);
}
}; tick();
} fadeIn(el);

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);

IE10+

el.classList.add('show');
el.classList.remove('hide');
.show {
transition: opacity 400ms;
}
.hide {
opacity: 0;
}

Hide

jQuery

$(el).hide();

IE8+

el.style.display = 'none';

Show

jQuery

$(el).show();

IE8+

el.style.display = '';

Elements

Alternatives:

Add Class

jQuery

$(el).addClass(className);

IE8+

if (el.classList)
el.classList.add(className);
else
el.className += ' ' + className;

IE10+

el.classList.add(className);

After

jQuery

$(el).after(htmlString);

IE8+

el.insertAdjacentHTML('afterend', htmlString);

Append

jQuery

$(parent).append(el);

IE8+

parent.appendChild(el);

Before

jQuery

$(el).before(htmlString);

IE8+

el.insertAdjacentHTML('beforebegin', htmlString);

Children

jQuery

$(el).children();

IE8+

var children = [];
for (var i=el.children.length; i--;){
// Skip comment nodes on IE8
if (el.children[i].nodeType != 8)
children.unshift(el.children[i]);
}

IE9+

el.children

Clone

jQuery

$(el).clone();

IE8+

el.cloneNode(true);

Contains

jQuery

$.contains(el, child);

IE8+

el !== child && el.contains(child);

Contains Selector

jQuery

$(el).find(selector).length

IE8+

el.querySelector(selector) !== null

Each

jQuery

$(selector).each(function(i, el){

});

IE8+

function forEachElement(selector, fn) {
var elements = document.querySelectorAll(selector);
for (var i = 0; i < elements.length; i++)
fn(elements[i], i);
} forEachElement(selector, function(el, i){ });

IE9+

var elements = document.querySelectorAll(selector);
Array.prototype.forEach.call(elements, function(el, i){ });

Empty

jQuery

$(el).empty();

IE8+

el.innerHTML = '';

Filter

jQuery

$(selector).filter(filterFn);

IE8+

function filter(selector, filterFn) {
var elements = document.querySelectorAll(selector);
var out = [];
for (var i = elements.length; i--;) {
if (filterFn(elements[i]))
out.unshift(elements[i]);
}
return out;
} filter(selector, filterFn);

IE9+

Array.prototype.filter.call(document.querySelectorAll(selector), filterFn);

Finding Children

jQuery

$(el).find(selector);

IE8+

el.querySelectorAll(selector);

Finding Elements

jQuery

$('.my #awesome selector');

IE8+

document.querySelectorAll('.my #awesome selector');

Get Style

jQuery

$(el).css(ruleName);

IE8+

// Varies based on the properties being retrieved, some can be retrieved from el.currentStyle
// https://github.com/jonathantneal/Polyfills-for-IE8/blob/master/getComputedStyle.js

IE9+

getComputedStyle(el)[ruleName]

Getting Attributes

jQuery

$(el).attr('tabindex');

IE8+

el.getAttribute('tabindex');

Getting Html

jQuery

$(el).html();

IE8+

el.innerHTML

Getting Outer Html

jQuery

$('<div>').append($(el).clone()).html();

IE8+

el.outerHTML

Getting Text

jQuery

$(el).text();

IE8+

el.textContent || el.innerText

IE9+

el.textContent

Has Class

jQuery

$(el).hasClass(className);

IE8+

if (el.classList)
el.classList.contains(className);
else
new RegExp('(^| )' + className + '( |$)', 'gi').test(el.className);

IE10+

el.classList.contains(className);

Matches

jQuery

$(el).is($(otherEl));

IE8+

el === otherEl

Matches Selector

jQuery

$(el).is('.my-class');

IE8+

var matches = function(el, selector) {
var _matches = (el.matches || el.matchesSelector || el.msMatchesSelector || el.mozMatchesSelector || el.webkitMatchesSelector || el.oMatchesSelector); if (_matches) {
return _matches.call(el, selector);
} else {
var nodes = el.parentNode.querySelectorAll(selector);
for (var i = nodes.length; i--;)
if (nodes[i] === el) {
return true;
}
return false;
} matches(el, '.my-class');

IE9+

var matches = function(el, selector) {
return (el.matches || el.matchesSelector || el.msMatchesSelector || el.mozMatchesSelector || el.webkitMatchesSelector || el.oMatchesSelector)(selector);
}; matches(el, '.my-class');

Next

jQuery

$(el).next();

IE8+

// nextSibling can include text nodes
function nextElementSibling(el) {
do { el = el.nextSibling; } while ( el && el.nodeType !== 1 );
return el;
} el.nextElementSibling || nextElementSibling(el);

IE9+

el.nextElementSibling

Offset

jQuery

$(el).offset();

IE8+

{left: el.offsetLeft, top: el.offsetTop}

Offset Parent

jQuery

$(el).offsetParent();

IE8+

el.offsetParent || el

Outer Height

jQuery

$(el).outerHeight()

IE8+

function outerHeight(el, includeMargin){
var height = el.offsetHeight; if(includeMargin){
var style = el.currentStyle || getComputedStyle(el);
height += parseInt(style.marginTop) + parseInt(style.marginBottom);
}
return height;
} outerHeight(el, true);

IE9+

function outerHeight(el, includeMargin){
var height = el.offsetHeight; if(includeMargin){
var style = getComputedStyle(el);
height += parseInt(style.marginTop) + parseInt(style.marginBottom);
}
return height;
} outerHeight(el, true);

Outer Width

jQuery

$(el).outerWidth()

IE8+

function outerWidth(el, includeMargin){
var height = el.offsetWidth; if(includeMargin){
var style = el.currentStyle || getComputedStyle(el);
height += parseInt(style.marginLeft) + parseInt(style.marginRight);
}
return height;
} outerWidth(el, true);

IE9+

function outerWidth(el, includeMargin){
var height = el.offsetWidth; if(includeMargin){
var style = getComputedStyle(el);
height += parseInt(style.marginLeft) + parseInt(style.marginRight);
}
return height;
} outerWidth(el, true);

Parent

jQuery

$(el).parent();

IE8+

el.parentNode

Prepend

jQuery

$(parent).prepend(el);

IE8+

parent.insertBefore(el, parent.firstChild);

Prev

jQuery

$(el).prev();

IE8+

// prevSibling can include text nodes
function prevElementSibling(el) {
do { el = el.prevSibling; } while ( el && el.nodeType !== 1 );
return el;
} el.prevElementSibling || prevElementSibling(el);

IE9+

el.prevElementSibling

Remove

jQuery

$(el).remove();

IE8+

el.parentNode.removeChild(el);

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'), ' ');

IE10+

el.classList.remove(className);

Replacing From Html

jQuery

$(el).replaceWith(string);

IE8+

el.outerHTML = string

Set Style

jQuery

$(el).css('border-width', '20px');

IE8+

// Use a class if possible
el.style.borderWidth = '20px';

Setting Attributes

jQuery

$(el).attr('tabindex', 3);

IE8+

el.setAttribute('tabindex', 3);

Setting Html

jQuery

$(el).html(string);

IE8+

el.innerHTML = string;

Setting Text

jQuery

$(el).text(string);

IE8+

if (el.textContent !== undefined)
el.textContent = string;
else
el.innerText = string;

IE9+

el.textContent = string;

Siblings

jQuery

$(el).siblings();

IE8+

var siblings = Array.prototype.slice.call(el.parentNode.children);

for (var i = siblings.length; i--;) {
if (siblings[i] === el) {
siblings.splice(i, 1);
break;
}
}

IE9+

Array.prototype.filter.call(el.parentNode.children, function(child){
return child !== el;
});

Toggle Class

jQuery

$(el).toggleClass(className);

IE8+

if (el.classList) {
el.classList.toggle(className);
} else {
var classes = el.className.split(' ');
var existingIndex = -1;
for (var i = classes.length; i--;) {
if (classes[i] === className)
existingIndex = i;
} if (existingIndex >= 0)
classes.splice(existingIndex, 1);
else
classes.push(className); el.className = classes.join(' ');
}

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(' ');
}

IE10+

el.classList.toggle(className)

Events

Off

jQuery

$(el).off(eventName, eventHandler);

IE8+

function removeEventListener(el, eventName, handler) {
if (el.removeEventListener)
el.removeEventListener(eventName, handler);
else
el.detachEvent('on' + eventName, handler);
} removeEventListener(el, eventName, handler);

IE9+

el.removeEventListener(eventName, eventHandler);

On

jQuery

$(el).on(eventName, eventHandler);

IE8+

function addEventListener(el, eventName, handler) {
if (el.addEventListener) {
el.addEventListener(eventName, handler);
} else {
el.attachEvent('on' + eventName, handler);
}
} addEventListener(el, eventName, handler);

IE9+

el.addEventListener(eventName, eventHandler);

Ready

jQuery

$(document).ready(function(){

});

IE8+

function ready(fn) {
if (document.addEventListener) {
document.addEventListener('DOMContentLoaded', fn);
} else {
document.attachEvent('onreadystatechange', function() {
if (document.readyState === 'interactive')
fn();
});
}
}

IE9+

document.addEventListener('DOMContentLoaded', function(){

});

Trigger Custom

Alternatives:

jQuery

$(el).trigger('my-event', {some: 'data'});

IE8+

// Custom events are not natively supported, so you have to hijack a random
// event.
//
// Just use jQuery.

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');

IE8+

if (document.createEvent) {
var event = document.createEvent('HTMLEvents');
event.initEvent('change', true, false);
el.dispatchEvent(event);
} else {
el.fireEvent('onchange');
}

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

Array Each

jQuery

$.each(array, function(i, item){

});

IE8+

function forEach(array, fn) {
for (i = 0; i < array.length; i++)
fn(array[i], i);
} forEach(array, function(item, i){ });

IE9+

array.forEach(function(item, i){

});

Bind

jQuery

$.proxy(fn, context);

IE8+

fn.apply(context, arguments);

IE9+

fn.bind(context);

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);

Index Of

jQuery

$.inArray(item, array);

IE8+

function indexOf(array, item) {
for (var i = 0; i < array.length; i++) {
if (array[i] === item)
return i;
}
return -1;
} indexOf(array, item);

IE9+

array.indexOf(item);

Is Array

jQuery

$.isArray(arr);

IE8+

isArray = Array.isArray || function(arr) {
return Object.prototype.toString.call(arr) == '[object Array]';
} isArray(arr);

IE9+

Array.isArray(arr);

Map

jQuery

$.map(array, function(value, index){

})

IE8+

function map(arr, fn) {
var results = []
for (var i = 0; i < arr.length; i++)
results.push(fn(arr[i], i))
return results
} map(array, function(value, index){ })

IE9+

array.map(function(value, index){

})

Now

jQuery

$.now();

IE8+

new Date().getTime();

IE9+

Date.now();

Parse Html

jQuery

$.parseHTML(htmlString)

IE8+

var parseHTML = function(str) {
var el = document.createElement('div')
el.innerHTML = str
return el.children
} parseHTML(htmlString)

IE9+

var parseHTML = function(str) {
var tmp = document.implementation.createHTMLDocument()
tmp.body.innerHTML = str
return tmp.body.children
} parseHTML(htmlString)

Parse Json

jQuery

$.parseJSON(string);

IE8+

JSON.parse(string);

Trim

jQuery

$.trim(string);

IE8+

string.replace(/^\s+|\s+$/g, '');

IE9+

string.trim();

Made by @adamfschwartz and @zackbloom at HubSpot.

转自:http://youmightnotneedjquery.com/

(转)You might not need jQuery的更多相关文章

  1. Angular杂谈系列1-如何在Angular2中使用jQuery及其插件

    jQuery,让我们对dom的操作更加便捷.由于其易用性和可扩展性,jQuer也迅速风靡全球,各种插件也是目不暇接. 我相信很多人并不能直接远离jQuery去做前端,因为它太好用了,我们以前做的东西大 ...

  2. jQuery UI resizable使用注意事项、实时等比例拉伸及你不知道的技巧

    这篇文章总结的是我在使用resizable插件的过程中,遇到的问题及变通应用的奇思妙想. 一.resizable使用注意事项 以下是我在jsfiddle上写的测试demo:http://jsfiddl ...

  3. Jquery的点击事件,三句代码完成全选事件

    先来看一下Js和Jquery的点击事件 举两个简单的例子 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...

  4. jQuery实践-网页版2048小游戏

    ▓▓▓▓▓▓ 大致介绍 看了一个实现网页版2048小游戏的视频,觉得能做出自己以前喜欢玩的小游戏很有意思便自己动手试了试,真正的验证了这句话-不要以为你以为的就是你以为的,看视频时觉得看懂了,会写了, ...

  5. jquery和Js的区别和基础操作

    jqery的语法和js的语法一样,算是把js升级了一下,这两种语法可以一起使用,只不过是用jqery更加方便 一个页面想要使用jqery的话,先要引入一下jqery包,jqery包从网上下一个就可以, ...

  6. jQuery之ajax实现篇

    jQuery的ajax方法非常好用,这么好的东西,你想拥有一个属于自己的ajax么?接下来,我们来自己做一个简单的ajax吧. 实现功能 由于jq中的ajax方法是用了内置的deferred模块,是P ...

  7. 利用snowfall.jquery.js实现爱心满屏飞

    小颖在上一篇一步一步教你用CSS画爱心中已经分享一种画爱心的方法,这次再分享一种方法用css画爱心,并利用snowfall.jquery.js实现爱心满屏飞的效果. 第一步: 利用伪元素before和 ...

  8. jQuery的61种选择器

    The Write Less , Do More ! jQuery选择器 1. #id : 根据给定的ID匹配一个元素 <p id="myId">这是第一个p标签< ...

  9. jquery.uploadify文件上传组件

    1.jquery.uploadify简介 在ASP.NET中上传的控件有很多,比如.NET自带的FileUpload,以及SWFUpload,Uploadify等等,尤其后面两个控件的用户体验比较好, ...

  10. 浅谈 jQuery 核心架构设计

    jQuery对于大家而言并不陌生,因此关于它是什么以及它的作用,在这里我就不多言了,而本篇文章的目的是想通过对源码简单的分析来讨论 jQuery 的核心架构设计,以及jQuery 是如何利用javas ...

随机推荐

  1. R 在linux redhat 6.5的编译安装过程

    下载源码包 在http://cran.r-project.org/mirrors.html,选择一个国内镜像下载需要的版本,比如:http://mirror.lzu.edu.cn/CRAN/src/b ...

  2. SQL语句小总结

    无论是面试过程中,还是未来工作中,SQL都是一定会考到和用到的.所以,在此对之前看过的一些SQL知识点进行一下总结和记录,算是起到一个笔记本的作用.没有深入学习过SQL的和对SQL印象不太深的朋友可以 ...

  3. Python 各进制间的转换(转)

    转载自:http://blog.chinaunix.net/uid-21516619-id-1824975.html python 2.6以后内置函数#10进制转为2进制>>> bi ...

  4. Inventor 2014 sp1/sp2激活

    问题: win8.1 inventor2014,不打补丁不兼容: 打了补丁xforce激活始终无法成功. 解决: 1. 使用未打补丁之前的 adlmact.dll 和 adlmact_libFNP.d ...

  5. [Spring MVC] - JSON

    Spring MVC中使用JSON,先必需引用两个包:jackson-core-asl-1.9.13.jar.jackson-mapper-asl-1.9.13.jar 因为需要使用到jquery测试 ...

  6. html5之history对象 控制浏览器前进或后退事件

    一.摘要: 总结用history对象操作浏览器的历史记录的方法,在项目中使用的是mui框架,总结中包括我在实际项目中遇到的问题. 二.总结: 实现效果: 实现代码: 上面的编辑页面加载的时候就要先调用 ...

  7. C#扇形的绘制与Hittest交互、图种制作

    C# 扇区的绘制与交互 哪位大神的杰作,代码里有,我就不废话了.源码下载方式如下. 注:将下面的图下载后,另存为.rar就可以看到图片中隐藏的代码程序了. 图种的制作 图种的目的在于隐藏文件.使得外人 ...

  8. C#功能杂集

    使用unsafe代码 Unsafe, fixed, stackalloc 由于C#可以使用元数据,验证函数签名.对象类型,保证执行过程的安全,如果要使用指针,则不能进行验证,用unsafe表示.uns ...

  9. ORA-12519, ORA-00020异常产生原因及解决方案

    近期在做项目的过程中,使用oracle时碰到了如下两个异常: ORA-12519, TNS:no appropriate service handler found: ORA-00020:maximu ...

  10. C# 系统错误日志处理类

    编写软件,难免会有一些异常,针对异常我们在实际的开发中相比都有一些,捕获异常的处理办法.把软件运行错误信息写成一个 错误日志文件很有必要.当我们在客户那边安装调试时就会更加快捷的,知道错误在哪里.否则 ...