You-Dont-Need-jQuery README

The project's README from github.com/camsong/You-Dont-Need-jQuery. As one markdown file: README.md.

You (Might) Don't Need jQuery

Frontend environments evolve rapidly nowadays and modern browsers have already implemented a great deal of DOM/BOM APIs which are good enough for production use. We don't have to learn jQuery from scratch for DOM manipulation or event handling. In the meantime, thanks to the spread of frontend libraries such as React, Angular and Vue, manipulating the DOM directly becomes anti-pattern, so that jQuery usage has never been less important. This project summarizes most of the alternatives in native JavaScript implementation to jQuery methods.

Snippets target current evergreen browsers (Chrome, Edge, Firefox, Safari). Internet Explorer is no longer supported by Microsoft, so IE-specific fallbacks have been removed. If you still need them, see the last IE-compatible version.

ℹ️ Notice:

  1. jQuery is still a great library and has many valid use cases. Don’t migrate away if you don’t want to!
  2. The alternatives are not completely equivalent in all scenarios, and it is recommended that you test it before using it.

Table of Contents

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

Translations

Query Selector

In place of common selectors like class, id or attribute we can use document.querySelector or document.querySelectorAll for substitution. The differences lie in:

Notice: document.getElementById, document.getElementsByClassName and document.getElementsByTagName are slightly faster than querySelector*, but getElementsBy* return a live HTMLCollection that changes as the DOM changes. Prefer querySelector* unless you have measured a bottleneck.

⬆ back to top

CSS & Style

⬆ back to top

DOM Manipulation

⬆ back to top

Ajax

Fetch API is the standard replacement for XMLHttpRequest and works in all modern browsers. Unlike $.ajax, fetch does not reject on HTTP error status such as 404 or 500; check response.ok yourself. For JSONP, try fetch-jsonp.

⬆ back to top

Events

⬆ back to top

Utilities

Most of jQuery utilities are also found in the native API. Other advanced functions could be chosen from better utilities libraries, focusing on consistency and performance. Lodash and es-toolkit are recommended replacements.

⬆ back to top

Promises

A promise represents the eventual result of an asynchronous operation. jQuery has its own way to handle promises. Native JavaScript implements a thin and minimal API to handle promises according to the Promises/A+ specification, and async/await makes them read like synchronous code.

⬆ back to top

Animation

The Web Animations API (el.animate()) is the closest native match to jQuery effects: it takes a duration in milliseconds, runs off the main thread where possible, and returns an Animation whose finished promise resolves when it ends.

Alternatives

Browser Support

Chrome Edge Firefox Safari Opera
Latest ✔ Latest ✔ Latest ✔ Latest ✔ Latest ✔

A few snippets use newer APIs: Promise.withResolvers() (2024), el.replaceChildren() (2020) and AbortSignal.timeout() (2022). Check Baseline if you support older browsers.

License

MIT

You-Dont-Need-jQuery (camsong/You-Dont-Need-jQuery)

Searching Database…

这是什么?这是一份参考指南,展示常见 jQuery 方法对应的原生 JavaScript(原生 DOM/BOM)写法,涵盖查询、样式、DOM 操作、Ajax、事件、工具函数、Promise 和动画。
这是我需要安装的库,还是只是参考文档?它不是需要安装的库,而是按主题(选择器、CSS、DOM、Ajax、事件、工具函数、Promise、动画)整理的文档和代码片段合集,你可以照着其中的写法复制使用。
我该怎么用?在目录中找到你需要的 jQuery 功能(例如 query selector、ajax、events),然后复制旁边显示的原生 JS 代码片段即可。
使用这些代码片段需要 jQuery 或其他库吗?不需要,这正是它的核心意义:代码片段只使用浏览器原生 API,例如 querySelector、fetch 和 Web Animations API。
每个代码片段的行为都会和它所替代的 jQuery 方法完全一致吗?不会。项目本身就提醒,这些替代写法并非在所有场景下都完全等效,因此在生产环境中使用前应先充分测试。
它还支持 Internet Explorer 吗?不支持。微软停止支持之后,IE 专用的兼容代码已被移除;如有需要,README 中还链接了一个兼容 IE 的旧版本。
读完之后,我应该完全停止使用 jQuery 吗?不必。README 明确指出 jQuery 仍然是一个很好的库,有其合理的使用场景,如果你愿意,继续使用它完全没有问题。
它涵盖了 Ajax 的替代方案吗?涵盖。它推荐用 Fetch API 替代 XMLHttpRequest,并指出与 $.ajax 不同,fetch 遇到 404、500 等 HTTP 错误状态时不会进入 reject(拒绝)状态。
jQuery 的工具函数,比如 extend 或 each,怎么办?大多数都有对应的原生写法(例如 Object.assign、展开运算符,以及数组方法);更复杂的工具函数,指南则建议改用 Lodash 或 es-toolkit。
不用 jQuery 时,动画怎么实现?它推荐使用 Web Animations API(el.animate()),这是最接近 jQuery 动画效果的原生方案,动画在主线程之外运行,并附带一个 finished Promise。
Creator
camsong
Size
0.9 MB
Stars
20.1K
Age
10y
Commits · 90 Days