简体   繁体   中英

How to add class to img element in array?

Simply trying to add a class to an img in an array

aftArray = $('.aft'); // creates array of HTML imgs
aftArray[0].addClass('currentTest'); // should change CSS by adding this class

Here is the resulting error from FF

[12:53:18.593] TypeError: aftArray[0].addClass is not a function...

WTF? Thanks!

You are trying to call addClass on DOM object instead of jQuery object. Indexer gives you do ojbect You need to use eq() to get jQuery object.

aftArray.eq(0).addClass('currentTest');

You do not have to iterate in this particular case.

$('.aft').addClass('currentTest');

because [0] gives you the DOM element! You want to use .eq(0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM