Is there an easy way to convert jquery code to javascript? [closed]

南楼画角 提交于 2019-11-26 04:08:37

问题


There are lots of examples out there of achieving certain things in javascript/dom using jQuery. However, using jQuery is not always an option which can make understanding the examples of javascript solutions written in jQuery hard.

Is there an easy way to convert jQuery code to regular javascript? I guess without having to access or understand the jQuery source code.


回答1:


The easiest way is to just learn how to do DOM traversing and manipulation with the plain DOM api (you would probably call this: normal JavaScript).

This can however be a pain for some things. (which is why libraries were invented in the first place).

Googling for "javascript DOM traversing/manipulation" should present you with plenty of helpful (and some less helpful) resources.

The articles on this website are pretty good: http://www.htmlgoodies.com/primers/jsp/

And as Nosredna points out in the comments: be sure to test in all browsers, because now jQuery won't be handling the inconsistencies for you.




回答2:


This will get you 90% of the way there ; )

window.$ = document.querySelectorAll.bind(document)

For Ajax, the Fetch API is now supported on the current version of every major browser. For $.ready(), DOMContentLoaded has near universal support. You Might Not Need jQuery gives equivalent native methods for other common jQuery functions.

Zepto offers similar functionality but weighs in at 10K zipped. There are custom Ajax builds for jQuery and Zepto as well as some micro frameworks, but jQuery/Zepto have solid support and 10KB is only ~1 second on a 56K modem.




回答3:


I just found this quite impressive tutorial about jquery to javascript conversion from Jeffrey Way on Jan 19th 2012 *Copyright © 2014 Envato* :

http://net.tutsplus.com/tutorials/javascript-ajax/from-jquery-to-javascript-a-reference/

Whether we like it or not, more and more developers are being introduced to the world of JavaScript through jQuery first. In many ways, these newcomers are the lucky ones. They have access to a plethora of new JavaScript APIs, which make the process of DOM traversal (something that many folks depend on jQuery for) considerably easier. Unfortunately, they don’t know about these APIs!

In this article, we’ll take a variety of common jQuery tasks, and convert them to both modern and legacy JavaScript.

I proposed it in a comment to OP, and after his suggestion, i publish it has an answer for everyone to refer to.

Also, Jeffrey Way mentioned about his inspiration witch seems to be a good primer for understanding : http://sharedfil.es/js-48hIfQE4XK.html

Has a teaser, this document comparison of jQuery to javascript :

$(document).ready(function() {
  // code…
});

document.addEventListener("DOMContentLoaded", function() {
  // code…
});

$("a").click(function() {
  // code…
})

[].forEach.call(document.querySelectorAll("a"), function(el) {
  el.addEventListener("click", function() {
    // code…
  });
});

You should take a look.




回答4:


I know this is an old thread but there is an excellent resource showing the native javascript equivalent to jquery, it even includes ES6 examples. This is the most comprehensive one I have found out of the other articles covering this subject.

jQuery to Vanilla JS




回答5:


If you want to learn javascript, watch these Douglas Crockford videos. They are very good.

  1. The Javascript programming language: History, Language, Advanced Features, Platforms, Standards, Style [download slides]
    • See YUI blog entry
  2. Theory of the DOM [download slides]
    • See YUI blog entry
  3. Advanced Javascript: Inheritance, modules, debugging, efficiency, JSON [download slides]
    • See YUI blog entry



回答6:


Is there an easy way to convert jQuery code to regular javascript?

No, especially if:

understanding the examples of javascript solutions written in jQuery [is] hard.

JQuery and all the frameworks tend to make understanding the code easier. If that's difficult to understand, then vanilla javascript will be torture :)




回答7:


I can see a reason, unrelated to the original post, to automatically compile jQuery code into standard JavaScript:

16k -- or whatever the gzipped, minified jQuery library is -- might be too much for your website that is intended for a mobile browser. The w3c is recommending that all HTTP requests for mobile websites should be a maximum of 20k.

w3c specs for mobile

So I enjoy coding in my nice, terse, chained jQuery. But now I need to optimize for mobile. Should I really go back and do the difficult, tedious work of rewriting all the helper functions I used in the jQuery library? Or is there some kind of convenient app that will help me recompile?

That would be very sweet. Sadly, I don't think such a thing exists.




回答8:


Jeremy Keith's book "DOM Scripting" is a great intro to working with Javascript and the DOM. I highly recommend it, whether you want to use jQuery or not. It's good to know what's going on beneath.




回答9:


There are several good books available. I like ppk on JavaScript. Here's Chapter 8 on the DOM.



来源:https://stackoverflow.com/questions/978799/is-there-an-easy-way-to-convert-jquery-code-to-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!