You Don't Need jQuery - Go语言中文社区

You Don't Need jQuery


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

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

目录

  1. Translations
  2. Query Selector
  3. CSS & Style
  4. DOM Manipulation
  5. Ajax
  6. Events
  7. Utilities
  8. Alternatives
  9. Browser Support

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

⬆ 回到顶部

CSS & Style

⬆ 回到顶部

DOM Manipulation

⬆ 回到顶部

Ajax

用 fetch 和 fetch-jsonp 替代

⬆ 回到顶部

Events

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

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢