oneuijs/You-Dont-Need-jQuery

 https://github.com/oneuijs/You-Dont-Need-jQuery/blob/master/README.zh-CN.md

You Don't Need jQuery

前端发展很快,现代浏览器原生 API 已经足够好用。我们并不需要为了操作 DOM、Event 等再学习一下 jQuery 的 API。同时由于 React、Angular、Vue 等框架的流行,直接操作 DOM 不再是好的模式,jQuery 使用场景大大减少。本项目总结了大部分 jQuery API 替代的方法,暂时只支持 IE10+ 以上浏览器。

Translations

Query Selector

常用的 class、id、属性 选择器都可以使用 document.querySelector 或 document.querySelectorAll 替代。区别是

  • document.querySelector 返回第一个匹配的 Element
  • document.querySelectorAll 返回所有匹配的 Element 组成的 NodeList。它可以通过 [].slice.call() 把它转成 Array
  • 如果匹配不到任何 Element,jQuery 返回空数组 [],但 document.querySelector 返回 null,注意空指针异常。当找不到时,也可以使用 || 设置默认的值,如 document.querySelectorAll(selector) || []

注意:document.querySelector 和 document.querySelectorAll 性能很。如果想提高性能,尽量使用 document.getElementByIddocument.getElementsByClassName 或 document.getElementsByTagName

  • 1.0 Query by selector

    // jQuery
    (selector); // Native
    document.querySelectorAll(selector);
  • 1.1 Query by class

    // jQuery
    (); // Native
    document.querySelectorAll(); document.getElementsByClassName();
  • 1.2 Query by id

    // jQuery
    (); // Native
    document.querySelector(); document.getElementById();
  • 1.3 Query by attribute

    // jQuery
    (a[target=_blank]); // Native
    document.querySelectorAll(a[target=_blank]);
  • 1.4 Find sth.

    • Find nodes

      // jQuery
      .(); // Native
      .querySelectorAll();
    • Find body

      // jQuery
      (); // Native
      document.;
    • Find Attribute

      // jQuery
      .(); // Native
      .getAttribute();
    • Find data attribute

      // jQuery
      .(); // Native
      // using getAttribute
      .getAttribute(data-foo);
      // you can also use `dataset` if only need to support IE 11+
      .dataset[];
  • 1.5 Sibling/Previous/Next Elements

    • Sibling elements

      // jQuery
      .siblings(); // Native
      [].filter.(.parentNode.children, function(child) {
      return child el;
      });
    • Previous elements

      // jQuery
      .(); // Native
      .previousElementSibling;
    • Next elements

      // next
      .();
      .nextElementSibling;
  • 1.6 Closest

    Closest 获得匹配选择器的第一个祖先元素,从当前元素开始沿 DOM 树向上。

    // jQuery
    .closest(queryString); // Native
    function closest(, selector) {
    const matchesSelector .matches .webkitMatchesSelector .mozMatchesSelector .msMatchesSelector; while (el) {
    (matchesSelector.(el, selector)) {
    return el;
    } {
    el .parentElement;
    }
    }
    return ;
    }
  • 1.7 Parents Until

    获取当前每一个匹配元素集的祖先,不包括匹配元素的本身。

    // jQuery
    .parentsUntil(selector, filter); // Native
    function parentsUntil(, selector, filter) {
    const result [];
    const matchesSelector .matches .webkitMatchesSelector .mozMatchesSelector .msMatchesSelector; // match start from parent
    el .parentElement;
    while (el matchesSelector.(el, selector)) {
    (filter) {
    result.(el);
    } {
    (matchesSelector.(el, filter)) {
    result.(el);
    }
    }
    el .parentElement;
    }
    return result;
    }
  • 1.8 Form

    • Input/Textarea

      // jQuery
      (#my-input).(); // Native
      document.querySelector(#my-input).value;
    • Get index of e.currentTarget between .radio

      // jQuery
      (.currentTarget).index(.radio); // Native
      [].indexOf.(document.querySelectorAll(.radio), .currentTarget);
  • 1.9 Iframe Contents

    jQuery 对象的 iframe contents() 返回的是 iframe 内的 document

    • Iframe contents

      // jQuery
      $iframe.contents(); // Native
      iframe.contentDocument;
    • Iframe Query

      // jQuery
      $iframe.contents().(); // Native
      iframe.contentDocument.querySelectorAll();

⬆ 回到顶部

CSS & Style

  • 2.1 CSS

    • Get style

      // jQuery
      .(color); // Native
      // 注意:此处为了解决当 style 值为 auto 时,返回 auto 的问题
      const .ownerDocument.defaultView;
      // null 的意思是不返回伪类元素
      .getComputedStyle(el, ).color;
    • Set style

      // jQuery
      .({ color #ff0011 }); // Native
      .style.color #ff0011;
    • Get/Set Styles

      注意,如果想一次设置多个 style,可以参考 oui-dom-utils 中 setStyles 方法

    • Add class

      // jQuery
      .addClass(className); // Native
      .classList.(className);
    • Remove class

      // jQuery
      .removeClass(className); // Native
      .classList.remove(className);
    • has class

      // jQuery
      .hasClass(className); // Native
      .classList.contains(className);
    • Toggle class

      // jQuery
      .toggleClass(className); // Native
      .classList.toggle(className);
  • 2.2 Width & Height

    Width 与 Height 获取方法相同,下面以 Height 为例:

    • Window height

      // jQuery
      (window).height(); // Native
      // 不含 scrollbar,与 jQuery 行为一致
      window.document.documentElement.clientHeight;
      // 含 scrollbar
      window.innerHeight;
    • Document height

      // jQuery
      (document).height(); // Native
      document.documentElement.scrollHeight;
    • Element height

      // jQuery
      .height(); // Native
      // 与 jQuery 一致(一直为 content 区域的高度)
      function getHeight() {
      const styles .getComputedStyle(el);
      const height .offsetHeight;
      const borderTopWidth parseFloat(styles.borderTopWidth);
      const borderBottomWidth parseFloat(styles.borderBottomWidth);
      const paddingTop parseFloat(styles.paddingTop);
      const paddingBottom parseFloat(styles.paddingBottom);
      return height borderBottomWidth borderTopWidth paddingTop paddingBottom;
      }
      // 精确到整数(border-box 时为 height 值,content-box 时为 height + padding + border 值)
      .clientHeight;
      // 精确到小数(border-box 时为 height 值,content-box 时为 height + padding + border 值)
      .getBoundingClientRect().height;
    • Iframe height

      $iframe .contents() 方法返回 iframe 的 contentDocument

      // jQuery
      (iframe).contents().height(); // Native
      iframe.contentDocument.documentElement.scrollHeight;
  • 2.3 Position & Offset

    • Position

      // jQuery
      .position(); // Native
      { left .offsetLeft, top .offsetTop }
    • Offset

      // jQuery
      .offset(); // Native
      function getOffset () {
      const .getBoundingClientRect(); return {
      top . window.pageYOffset document.documentElement.clientTop,
      left . window.pageXOffset document.documentElement.clientLeft
      }
      }
  • 2.4 Scroll Top

    // jQuery
    (window).scrollTop(); // Native
    (document.documentElement document.documentElement.scrollTop) document..scrollTop;

⬆ 回到顶部

DOM Manipulation

  • 3.1 Remove

    // jQuery
    .remove(); // Native
    .parentNode.removeChild(el);
  • 3.2 Text

    • Get text

      // jQuery
      .(); // Native
      .textContent;
    • Set text

      // jQuery
      .(string); // Native
      .textContent string;
  • 3.3 HTML

    • Get HTML

      // jQuery
      .(); // Native
      .innerHTML;
    • Set HTML

      // jQuery
      .(htmlString); // Native
      .innerHTML htmlString;
  • 3.4 Append

    Append 插入到子节点的末尾

    // jQuery
    .append(<div id='container'>hello</div>); // Native
    newEl document.createElement();
    newEl.setAttribute(, container);
    newEl.innerHTML hello;
    .appendChild(newEl);
  • 3.5 Prepend

    // jQuery
    .prepend(<div id='container'>hello</div>); // Native
    newEl document.createElement();
    newEl.setAttribute(, container);
    newEl.innerHTML hello;
    .insertBefore(newEl, .firstChild);
  • 3.6 insertBefore

    在选中元素前插入新节点

    // jQuery
    $newEl.insertBefore(queryString); // Native
    const target document.querySelector(queryString);
    target.parentNode.insertBefore(newEl, target);
  • 3.7 insertAfter

    在选中元素后插入新节点

    // jQuery
    $newEl.insertAfter(queryString); // Native
    const target document.querySelector(queryString);
    target.parentNode.insertBefore(newEl, target.nextSibling);

⬆ 回到顶部

用 fetch 和 fetch-jsonp 替代

⬆ 回到顶部

Events

完整地替代命名空间和事件代理,链接到 https://github.com/oneuijs/oui-dom-events

  • 5.1 Bind an event with on

    // jQuery
    .(eventName, eventHandler); // Native
    .addEventListener(eventName, eventHandler);
  • 5.2 Unbind an event with off

    // jQuery
    .(eventName, eventHandler); // Native
    .removeEventListener(eventName, eventHandler);
  • 5.3 Trigger

    // jQuery
    (el).trigger(custom-event, {key1 }); // Native
    (window.CustomEvent) {
    const event CustomEvent(custom-event, {detail {key1 }});
    } {
    const event document.createEvent(CustomEvent);
    event.initCustomEvent(custom-event, , , {key1 });
    } .dispatchEvent(event);

⬆ 回到顶部

Utilities

  • 6.1 isArray

    // jQuery
    .isArray(range); // Native
    Array.isArray(range);
  • 6.2 Trim

    // jQuery
    .(string); // Native
    string.();
  • 6.3 Object Assign

    继承,使用 object.assign polyfill https://github.com/ljharb/object.assign

    // jQuery
    .extend({}, defaultOpts, opts); // Native
    Object.assign({}, defaultOpts, opts);
  • 6.4 Contains

    // jQuery
    .contains(el, child); // Native
    el child .contains(child);

⬆ 回到顶部

Alternatives

Browser Support

Latest ✔ Latest ✔ 10+ ✔ Latest ✔ 6.1+ ✔

License

oneuijs/You-Dont-Need-jQuery的更多相关文章

  1. You Don't Need jQuery

    前端发展很快,现代浏览器原生 API 已经足够好用.我们并不需要为了操作 DOM.Event 等再学习一下 jQuery 的 API.同时由于 React.Angular.Vue 等框架的流行,直接操 ...

  2. 抛弃jQuery,拥抱原生JavaScript

    前端发展很快,现代浏览器原生 API 已经足够好用.我们并不需要为了操作 DOM.Event 等再学习一下 jQuery 的 API.同时由于 React.Angular.Vue 等框架的流行,直接操 ...

  3. 原生JS替代jQuery的各种方法汇总

    前端发展很快,现代浏览器原生 API 已经足够好用.我们并不需要为了操作 DOM.Event 等再学习一下 jQuery 的 API.同时由于 React.Angular.Vue 等框架的流行,直接操 ...

  4. jQuery与原生JS相互转化

    前端发展很快,现代浏览器原生 API 已经足够好用.我们并不需要为了操作 DOM.Event 等再学习一下 jQuery 的 API.同时由于 React.Angular.Vue 等框架的流行,直接操 ...

  5. 你不需要jQuery You Don't Need jQuery

    转载:https://github.com/oneuijs/You-Dont-Need-jQuery/blob/master/README.zh-CN.md You Don't Need jQuery ...

  6. 原生js替换jQuery各种方法-中文版

    原文https://github.com/nefe/You-D... 原生JS与jQuery操作DOM对比 You Don't Need jQuery 前端发展很快,现代浏览器原生 API 已经足够好 ...

  7. You-Dont-Need-JQuery (你不需要JQuery)

    看完这篇文章我才觉得真的要用JQuery ,因为实在是有些地方设计的使用太复杂了, document.querySelector() 和 Document.querySelectorAll 的确是很方 ...

  8. 原生 JavaScript 代替 jQuery【转】

    目录 用原生JavaScript代替jQuery Query Selector CSS & Style DOM Manipulation Ajax Events Utilities Promi ...

  9. 去JQUERY化

    时间 2016-05-17 12:43:59  OurJS 原文  http://ourjs.com/detail/573a9cec88feaf2d031d24fc 主题 jQuery 这是一篇使用原 ...

随机推荐

  1. 基于物理渲染的渲染器Tiberius计划

    既然决定实现一个光栅化软件渲染器,我又萌生了一个念头:实现一个基于物理渲染的渲染器.

  2. linux 学习随笔-压缩和解压缩

    .gz 由gzip压缩工具压缩的文件 .bz2 由bzip2压缩工具压缩的文件 .tar 由tar打包程序打包的文件 .tar.gz 先由tar打包,gzip压缩 .tar.bz2 先由tar打包,b ...

  3. Windows on Device 项目实践 3 - 火焰报警器制作

    在前两篇<Windows on Device 项目实践 1 - PWM调光灯制作>和<Windows on Device 项目实践 2 - 感光灯制作>中,我们学习了如何利用I ...

  4. PL/SQL Developer连接本地Oracle 11g 64位数据库

    转摘:http://www.cnblogs.com/ymj126/p/3712727.html 用于学习,笔记,以备后用. 1.登录PL/SQL Developer 这里省略Oracle数据库和PL/ ...

  5. tar解压问题gzip: stdin: not in gzip format

    如下所示,使用tar -zxvf解压文件时遇到"gzip: stdin: not in gzip format"等错误: [root@DB-Server tmp]# [root@D ...

  6. MS SQL 需要定期清理日志文件

    前言碎语 关于对SQL SERVER 日志文件管理方面了解不多的话,可以参考我的这篇博客文章“MS SQL 日志记录管理”,不过这篇文章只是介绍对SQL SERVER日志记录的深入认知了解,并没有提出 ...

  7. Win10系统旗舰版ghost版系统镜像下载

    微软已经发布了Win10预览版10041快速版更新,但通过Windows更新的方式比较慢.现在微软官方已经发布Win10预览版10041的系统ISO镜像,还没更新这一版本的朋友可以使用该镜像进行更新. ...

  8. JAVA JVM虚拟机选项:Xms Xmx PermSize MaxPermSize 区别

    Xms : 是指设定程序启动时占用内存大小.一般该值设置大的会使程序启动快,但是可能会使本机暂时变慢. Xmx : 是指设定程序运行期间最大可占用的内存大小,如果程序运行需要占用更多的内存,超出这个 ...

  9. nginx下配置404错误页面

    1.创建自己的404.html页面,并放于网站根目录. 2.更改nginx.conf在http定义区域加入: fastcgi_intercept_errors on; 3.更改nginx.conf(或 ...

  10. 树莓派搭建ActiveMQ

    树莓派上安装ActiveMQ和在其它Linux发行版基本相同,只是在开防火墙端口时有区别.   硬件信息: 树莓派3B型,Raspbian系统   安装 //下载ActiveMQ安装包 http:// ...