Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

javascript - jQuery循环使用相同类的元素(jQuery to loop through elements with the same class)

I have a load of divs with the class testimonial and I want to use jquery to loop through them to check for each div if a specific condition is true.

(我有一个带有类testimonial的div的加载,我想使用jquery循环遍历它们以检查每个div,如果特定条件为真。)

If it is true, it should perform an action.

(如果是,它应该执行一个动作。)

Does anyone know how I would do this?

(有谁知道我会怎么做?)

  ask by geoffs3310 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use each: ' i ' is the postion in the array, obj is the DOM object that you are iterating (can be accessed through the jQuery wrapper $(this) as well).

(使用each:' i '是数组中的postion, obj是你正在迭代的DOM对象(也可以通过jQuery包装器$(this)访问)。)

$('.testimonial').each(function(i, obj) {
    //test
});

Check the api reference for more information.

(有关更多信息,请查看api参考 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...