简体   繁体   English

使用Javascript For循环使用Jquery选择类

[英]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. 我正在尝试创建一个for循环,该循环将使用for循环的类的元素。 I know in python you can go "for element in thing " and was wondering if there was something similar in Javascript. 我知道在python中,您可以“针对事物中的元素”,并且想知道Javascript中是否存在类似内容。 I found some information online about the javascript for/in loop and have the following code written: 我在线上找到了有关javascript for / in循环的信息,并编写了以下代码:

    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. 基本上,我很难找出将星号放在何处以及是否可以使用Javascript来完成这种语法。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Since you tagged jquery ,You can loop through the class elements like 由于标记了jquery,因此可以遍历class元素

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

Learn more here on each() 在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: 您使用jQuery标记了这个问题,因此我假设您可以在脚本的这一点访问它:使用jQuery的each函数最简单的方法是:

$('.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
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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