繁体   English   中英

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

[英]Selecting a class with Jquery using a Javascript For loop

好的,所以我将一个类应用于一组图像。 我正在尝试创建一个for循环,该循环将使用for循环的类的元素。 我知道在python中,您可以“针对事物中的元素”,并且想知道Javascript中是否存在类似内容。 我在线上找到了有关javascript for / in循环的信息,并编写了以下代码:

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

基本上,我很难找出将星号放在何处以及是否可以使用Javascript来完成这种语法。 任何帮助将不胜感激。

由于标记了jquery,因此可以遍历class元素

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

在each()上了解更多

您使用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