$.each(obj,function(key,value) {})
I am a newbie of jquery, i know there are two ways to write the structure like this:
$('document').ready(function(){}); or $(function(){});
why $.each() put . behind $ rather than $(each())
`
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In JavaScript (and remember, jQuery is a JavaScript library that doesn’t do anything you couldn’t do yourself in JavaScript) functions are a type of object. Hopefully you already know how to define and call a JavaScript function, with the following syntax that is similar to many other languages:
But because a function is also an object it can have other properties:
In that example
somePropertywas a simple string, but you can also create properties that refer to functions, i.e., that are methods:The jQuery syntax you have asked about is essentially the same thing as above, except at first it seems more complicated because the
$()function does different things depending on the type of the parameter(s) you pass to it. ButSome jQuery code gets a bit more confusing because in many cases jQuery function calls return a “jQuery object” that itself has a bunch of methods defined.
Many jQuery methods return the same type of jQuery object, so you can chain calls together:
The last
.show()at the end also returns a jQuery object but we aren’t doing anything with it.These concepts are all a standard part of the JavaScript language, so I suggest you work your way through a good JavaScript tutorial. If you look at the jQuery source code (i.e., the content of jQuery.js) there is a lot of confusing code to digest, but if you start by playing around with something simple like my first few examples above you’ll get the hang of it.