簡體   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