如何忘记jQuery而换用原生的JavaScriptAPI

 

转自github.com/roman01la

How to forget about jQuery and start using native JavaScript APIs

JavaScript is here and it’s ready for you, but probably you are not ready for it yet. Why not using jQuery? Because it’s slow and your website doesn’t really need extra weight.I’m not going to argue about libs vs native. Sometimes it’s really hard to live without all that magic stuff. But I’m going to argue about loading kilos of code just only for a one-character-selector-function aka $ or things like that.Assuming that shorthands is not the case, everyone use jSomething because of it’s support for IE, animation handling and the only selector function.

Native equivalents

Select elements

Create elements

Add event listener

Set/get attribute

Add/remove/toggle class

Append

Clone

Remove

Parent

Prev/next element

XHR aka AJAX

So this is just a few, you can find more native stuff using the console in your browser or read MDN’s JS API reference or WPD’s DOM docs.

You still can use libs, check here for some ultra-lightweight and find the one you need for particular task, but first make sure you can’t achieve the goal without it, otherwise use native JS.