您可能不需要jquery

2019-05-05106次阅读javascript原生Js

刚开始使用jQuery可以帮助我们免受浏览器不兼容的烦恼,但现代的浏览器更新发展超乎我们的想象,所以有些情况下根本不需要引入一个jQuery库文件。一味的使用jQuery,会让你忘了或失去很多原生Javascript的魅力。

 

AJAX篇

JSON

JQUERY

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

});

IE7+

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

request.onload = function() {
  if (request.status >= 200 && request.status < 400) {
    //成功
    //JSON.parse是IE8+
    var data = JSON.parse(request.responseText);
  } else {
    //我们到达了目标服务器,但它返回了一个错误

  }
};

request.onerror = function() {
  // 有某种连接错误


};

request.send();

Post

JQUERY

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

IE7+

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

Request

JQUERY

$.ajax({
  type: 'GET',
  url: '/my/url',
  success: function(resp) {

  },
  error: function() {

  }
});

IE7+

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

request.onload = function() {
  if (request.status >= 200 && request.status < 400) {
    // 成功!
    var resp = request.responseText;
  } else {
    //我们到达了目标服务器,但它返回了一个错误

  }
};

request.onerror = function() {
   // 有某种连接错误


};

request.send();

 

效果

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


Hide

JQUERY

$(el).hide();

IE6+

el.style.display = 'none';


Show

JQUERY

$(el).show();

IE6+

el.style.display = 'block';


HTML标签相关

Add Class

JQUERY

$(el).addClass(className);

IE6+

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


After

JQUERY

$(el).after(htmlString);

IE6+

el.insertAdjacentHTML('afterend', htmlString);

Append

JQUERY

$(parent).append(el);

IE6+

parent.appendChild(el);

Before

JQUERY

$(el).before(htmlString);

IE6+

el.insertAdjacentHTML('beforebegin', htmlString);

Children

JQUERY

$(el).children();

IE6+

el.children


Clone

JQUERY

$(el).clone();

IE6+

el.cloneNode(true);

Contains

JQUERY

$.contains(el, child);

IE6+

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

Contains Selector

JQUERY

$(el).find(selector).length;

IE8+

el.querySelector(selector) !== null


Each

JQUERY

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

});

IE9+

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

});


Empty

JQUERY

$(el).empty();

IE6+

el.innerHTML = '';


Filter

JQUERY

$(selector).filter(filterFn);

IE9+

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


Find Children

JQUERY

$(el).find(selector);

IE8+

el.querySelectorAll(selector);

Find Elements

JQUERY

$('.my #awesome selector');

IE8+

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


Get Attributes

JQUERY

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

IE6+

el.getAttribute('tabindex');

Get Html


JQUERY

$(el).html();

IE6+

el.innerHTML


Get Outer Html

JQUERY

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

IE6+

el.outerHTML


Get Style

JQUERY

$(el).css(ruleName);

IE9+

getComputedStyle(el)[ruleName];

 

Get Text

JQUERY

$(el).text();

IE9+

el.textContent


Has Class

JQUERY

$(el).hasClass(className);

IE6+

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


Matches

JQUERY

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

IE6+

el === otherEl


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

 

Next

JQUERY

$(el).next();

IE9+

el.nextElementSibling


Offset

JQUERY

$(el).offset();

IE6+

var rect = el.getBoundingClientRect();

{
  top: rect.top + document.body.scrollTop,
  left: rect.left + document.body.scrollLeft
}

 

Offset Parent

JQUERY

$(el).offsetParent();

IE6+

el.offsetParent || el


Outer Height

JQUERY

$(el).outerHeight();

IE6+

el.offsetHeight


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

JQUERY

$(el).outerWidth();

IE6+

el.offsetWidth


Outer Width With Margin

JQUERY

$(el).outerWidth(true);

IE9+

function outerWidth(el) {
  var width = el.offsetWidth;
  var style = getComputedStyle(el);

  width += parseInt(style.marginLeft) + parseInt(style.marginRight);
  return width;
}

outerWidth(el);

 

Parent

JQUERY

$(el).parent();

IE6+

el.parentNode


Position

JQUERY

$(el).position();

IE6+

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


Position Relative To Viewport

JQUERY

var offset = el.offset();

{
  top: offset.top - document.body.scrollTop,
  left: offset.left - document.body.scrollLeft
}

IE6+

el.getBoundingClientRect()


Prepend

JQUERY

$(parent).prepend(el);

IE6+

parent.insertBefore(el, parent.firstChild);

 

Prev

JQUERY

$(el).prev();

IE9+

el.previousElementSibling


Remove

JQUERY

$(el).remove();

IE6+

el.parentNode.removeChild(el);


Remove Class

JQUERY

$(el).removeClass(className);

IE6+

if (el.classList)
  el.classList.remove(className);
else
  el.className = el.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ');


Replace From Html

JQUERY

$(el).replaceWith(string);

IE6+

el.outerHTML = string;


Set Attributes

JQUERY

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

IE6+

el.setAttribute('tabindex', 3);


Set Html

JQUERY

$(el).html(string);

IE6+

el.innerHTML = string;


Set Style

JQUERY

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

IE6+

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


Set Text

JQUERY

$(el).text(string);

IE9+

el.textContent = string;


Siblings

JQUERY

$(el).siblings();

IE9+

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


Toggle Class

JQUERY

$(el).toggleClass(className);

IE6+

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

Off

JQUERY

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

IE9+

el.removeEventListener(eventName, eventHandler);


On

JQUERY

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

IE9+

el.addEventListener(eventName, eventHandler);


Ready

JQUERY

$(document).ready(function(){

});

IE9+

function ready(fn) {
  if (document.attachEvent ? document.readyState === "complete" : document.readyState !== "loading"){
    fn();
  } else {
    document.addEventListener('DOMContentLoaded', fn);
  }
}


Trigger Custom

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
var event = document.createEvent('HTMLEvents');
event.initEvent('change', true, false);
el.dispatchEvent(event);


其它

Bind

JQUERY

$.proxy(fn, context);

IE9+

fn.bind(context);


Array Each

JQUERY

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

});

IE9+

array.forEach(function(item, i){

});


Deep Extend

JQUERY

$.extend(true, {}, objA, objB);

IE6+

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')
          out[key] = deepExtend(out[key], obj[key]);
        else
          out[key] = obj[key];
      }
    }
  }

  return out;
};

deepExtend({}, objA, objB);


Extend

JQUERY

$.extend({}, objA, objB);


IE6+

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

IE9+

array.indexOf(item);


Is Array

JQUERY

$.isArray(arr);

IE9+

Array.isArray(arr);


Map

JQUERY

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

});

IE9+

array.map(function(value, index){

});


Now

JQUERY

$.now();

IE9+

Date.now();


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


Parse Json

JQUERY

$.parseJSON(string);

IE8+

JSON.parse(string);


Trim

JQUERY

$.trim(string);

IE9+

string.trim();


Type

JQUERY

$.type(obj);

IE6+

Object.prototype.toString.call(obj).replace(/^\[object (.+)\]$/, '$1').toLowerCase();

 

上一篇: Web Components即web组件HTML template模板  下一篇: JavaScript数组扁平化  

您可能不需要jquery相关文章