开发者

How do I assign event handlers to every object with a certain attribute?

Basically, I want to assign an event handler to each page number (in a ul) in some sort of loop. Each list item has a certain class and a name attribute. I w开发者_JAVA技巧ant to assign the event handler .click() and then inside access the name attribute. Thanks to anyone who responds/views this post, and I would be appreciative if you would also show how to access the name attribute. I can access all the list items like this I think (accessing each individual one is another process I don't know)

$('li[class|='pagelistitem'). //now I need to assign event handler and access 'name' attr


$('li[class|="pagelistitem"]').click(function() {
   var thisName = $(this).attr("name");
});

The above code selects all li elements with a class value with prefix "pagelistitem". It attaches a click event listener to each element in that set, and in the event handler function, it gets the name attribute of the clicked element.

As mentioned in the comments, you could (and should, in this case) use this.name rather than passing this into another jQuery object and calling the attr method. However, as you specifically mentioned getting the attribute in the question, I wanted to show you the use of the jQuery method.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜