简体   繁体   中英

Selecting a class with Jquery using a Javascript For loop

Ok, so I have a class applied to a group of images. I am trying to create a for loop that will use the elements of a class for the for loop. I know in python you can go "for element in thing " and was wondering if there was something similar in Javascript. I found some information online about the javascript for/in loop and have the following code written:

    function walkingFeet(){
        for (foot in *class named walkingFoot*){
            [code to be executed]
        }
    }

I basically am just having trouble finding out what to put where the asterisks are and if that sort of syntax is something that I can even do with Javascript. Any help would be greatly appreciated.

Since you tagged jquery ,You can loop through the class elements like

$('.yourClass').each(function() {
     alert(this.id);  
});

Learn more here on each()

You tagged this question with jQuery , so I assume you have access to it at this point of the script: It's easiest to be done with jQuery's each -function:

$('.element').each(function(index, element) {
    // index = the current index, the first would be 0
    // element = the html-dom-element, the same as 'this'

    // code to be executed
});

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