开发者

how to shift from JavaScript to jQuery

jQuery is a framework of javaScript. But, I very well able to code in javaScript. I am n开发者_Go百科ot getting how do I (re)code it in jQuery !

Is learning jQuery from beginning is only way or should I follow any other way ?

EDIT : is jQuery used only for AJAX ? or where can I find proper to use jQuery ?


You have to learn jQuery in order to use it, yes.

(You don't, however, use jQuery instead of JavaScript. You might use it instead of directly using DOM, XHR and so on, but it is JavaScript)


jQuery is JavaScript. It's just another tool. Its big benefit is that you can manipulate the DOM and perform AJAX requests much easier.

For example, to get all links by class name in JavaScript (using this function) is:

function getElementsByClassName(className, tag, elm){
    var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
    var tag = tag || "*";
    var elm = elm || document;
    var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
    var returnElements = [];
    var current;
    var length = elements.length;
    for(var i=0; i<length; i++){
        current = elements[i];
        if(testClass.test(current.className)){
            returnElements.push(current);
        }
    }
    return returnElements;
}

var elements = getElementsByClassName(document,'a','myclass');

The same using jQuery*:

var elements = $("a.myclass");

Of course, jQuery would do similar things behind the scenes but the point is that you don't need to know about it (and more importantly, don't need to rewrite it all). There are lots of people coming up with clever things for jQuery (and tests, so you know it works cross browser).

Use jsFiddle to quickly play with some examples in jQuery (look at the jQuery docs). Don't necessarily set out to rewrite your entire JavaScript using jQuery. Just start using it for DOM operations/AJAX requests (even something as simple as using the show or hide methods). Eventually you'll get used to using jQuery and eye up your older code to rewrite. Just to give you an example, I once rewrote a 200-300 line piece of JavaScript into about 20 lines with jQuery. The reason for the massive difference is because I was able to remove all the 'helper' functions (like getElementsByClassName one above) since jQuery handles that for me.

Good luck :)


* The pedantic among you will know it's not entirely the same, since it returns the jQuery object - but this can really be ignored because it'll do the same result.


Rewriting existing javascript code to take advantage of the jQuery framework will require you knowledge of jQuery. There's no other way. In fact there is: you could hire someone doing the job for you but I don't think this was your original question.


There's no easy answer on that question. Learn the fundamentals of jQuery then rewrite your code from scratch. As with all new languages/concepts start small and then get bolder..

But from someone who did the transition, I can say that the effort was worth it!

Enjoy learning jQuery!


My advice is don't go for recoding the entire javascript at first time.

First take a small parts in a javascript for show and hide the div or table or any other elements.Then you will do it by using Jquery.After getting success on this you will get some confidence.

Then go for ajax using Jquery.

By this way you can learning the Jquery and step by step you changed your javascript to jquery.

Hope this will help you.


I'm quite surprised this hasn't been mentioned before now. But one of the absolutely best sources for beginners is the well written and open sourced book jQuery Fundamentals by Rebecca Murphey.

It will walk you through the basics of both JavaScript and jQuery, and will hopefully be the only thing (together with the docs) you'll need to get you going with jQuery!

I also recommend this for more advanced users because it will help you break free of bad practices that you might have picked up from, I'm sad to say it, badly written tutorials or books.

Happy reading!


jQuery isnt very different. It just combines some of the best features of javascript and uses CSS selectors to give you a easy to use language. Where most of the typical lines of code can be converted into smaller jQuery statements just start following a few examples and u will by default start moving to learning jQuery more.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜