jQuery selection behaviour on arrays of DOM elements
Assuming a query as such.
var result = $(".ui-th-column");
This query in my case will return an array of objects as there are a number that match the selector provided.
If I then go something like.
result.attr("myAttributeName") == "MyExpectedValue"
I receive a result.
My question is this. Which element is jQuery acting on? As obviously different dom elements in this case my have different attributes yet it returns a value. Is it simply returning the value of the first element?
As a follow on to this question; assuming I now have my array of elements I wish to act on and assuming I wish to bind an event such as click to them all. Do I开发者_运维百科 need to use the each operation or can I simply go
result.click(function () {});
and all my elements will be wired up? I have tried to google this but wasn’t sure what terms to use.
Yes it is only getting the first element in the case of attr
.
Description: Get the value of an attribute for the first element in the set of matched elements.
From: http://api.jquery.com/attr/
No, you don't have to call each
on the click
method for binding event handlers, it will attach the handler to all elements in the set.
Take a look at the bind
method.
Description: Attach a handler to an event for the elements.
The click
method is shorthand...
The jQuery library provides shortcut methods for binding the standard event types, such as
.click()
for.bind('click')
.
精彩评论