开发者

What's the technical difference between jQuery arrays and jQuery objects?

Of these two, which is correct?

jQuery('someclass').click(fu开发者_运维问答nction() {
  alert("I've been clicked!");
});

or

jQuery('someclass').each().click(function() {
  alert("I've been clicked!");
});

I'm wondering if I have a fundamental misunderstanding of how certain aspects of jQuery work, or if it's just got a lot of tolerance built into each of its functions to allow either a single element or a collection of elements.


The first is correct, the second will fail as a callback function is not defined. If a callback function was defined and the click event handler applied to each iterated element inside of the callback in .each(), then the two would be functionally similar. However, the each call would really be redundant as jQuery comands/methods are generally applied to all matched elements in the jQuery object (there are certain methods such as .val() that do not, but they are the exception than the norm). This is done by internally applying .each() :)

The jQuery object is an object with array-like properties; all matched elements are indexed properties of the object i.e.

<p>Hello</p><World</p>

$('p'); // is an object { selector: 'p', O: [DOM Element], 1: [DOM Element], ... }

where [DOM Element] represents a reference to an HTMLElement in the DOM that matches the selector. The advantage of being array-like is that it makes performing array operations on the object very straightforward.


A jQuery object is a wrapper around an array of DOM elements. So a "jQuery array" is a jQuery object. The object contains every element on the page that matches the selector. Most methods you call on a jQuery object implicitly loop through all the elements in the array and apply to all of them.

or if it's just got a lot of tolerance built into each of its functions to allow either a single element or a collection of elements.

This is sort of the case. Really each method works on one element, but jQuery calls it as many times as it has objects to work on. I believe it actually uses .each() internally to do this, so if you understand how .each() works then you understand how jQuery's implicit looping works.

Some methods do not apply to every object in the collection, but the ones that don't say so in their documentation. The exceptions are methods that it wouldn't make sense to apply to every element. For example, if you call $('.blah').innerHeight() you'll get the height of the first element in the jQuery object returned by $('.blah.').

There are also methods that can both get and set attributes. When getting they return the value from the first element, and when setting they apply to all matched elements. For example, if you call $('.blah').css('font-size') you'll get the size from the first element in the collection, but if you call $('.blah').css('width', 100) you'll set the width style of every element in the jQuery collection.

Note that you can actually treat a jQuery object like an array. Just use array notation to access an element.

$jQ_obj = $('.classy').find('.refined');
element = $jQ_obj[3];

This would store the fourth element on the page that has the class refined and is a child of an element that has classy. The resulting object in element is a raw DOM object.

Second point,

Since the answer that mentioned this was deleted, make sure you understand how .each() works. You need to pass it the function you want to be applied to each matched element, just like the event handler. So it should be:

jQuery('.someclass').each(function(i) { //don't forget the dot!
    $(this).click(function() {
        alert("I've been clicked!");
    });
});

Inside the callback to .each() the this keyword refers to the current DOM element being looped through, and the arg passed to the callback is the loop index. this is a raw DOM element (think of it like writing $('.someclass')[i]) so you have to create a jQuery object from it to do call .click() on it.


 Var object={};
object.push{id:1,name:{("madan","jeevan","charan")},education:"Hyderabad"};

Object always returns only one row,,

var Arrayobj=[];
Arrayobj.push({id:1,name:"madna"},{id:2,name:"jeevan"},{id:3,name:"charan"});

//Array can return more than one row
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜