如何循环遍历jquery对象数组和.hide()中的每一个 [英] how to loop through an array of jquery objects and .hide() each of them

查看:99
本文介绍了如何循环遍历jquery对象数组和.hide()中的每一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遍历我创建的jquery对象数组,这些对象在单击时是隐藏的。现在我不知道我在哪里出错了,并且想知道是否有人可以解释我需要做些什么来使它工作?

I am trying to loop through an array of jquery objects I have created that are hidden when clicked. Right now Im not sure where I am going wrong with this and was wondering if anyone could explain what I need to do to make it work?

我在这里创造了一个小提琴 http://jsfiddle.net/hd5qa/3/

I have created a fiddle here http://jsfiddle.net/hd5qa/3/

所有建议非常感谢

Kyle

推荐答案

小提琴的问题如下:

1)你没有包含jQuery库。

1) You hadn't included the jQuery library.

2)你没有正确关闭每个循环(缺少括号)。

2) You hadn't closed the each loop correctly (missing parenthesis).

3)你错过了来自myButton的 id selector

3) You were missing the id selector # from "myButton".

这是更新,工作小提琴

但请注意,你可以这样做:

Note, however, that you could simply do this:

$("#blue, #red, #green, #black, #purple, #orange").hide();

或者更好的是,将所有这些放在包含的父元素中,只需隐藏它,或者使用 $(#parent div)。hide();

Or better, put all of those in a containing parent element, and simply hide that, or use $("#parent div").hide();.

您可以将该选择的结果存储在一个变量,而不是你当前拥有的6个变量,因为jQuery方法倾向于对匹配集中的每个元素进行操作而不需要循环。

You can store the result of that selection in a single variable, rather than the 6 you currently have, as jQuery methods tend to operate on each element in the matched set without the need for a loop.

这篇关于如何循环遍历jquery对象数组和.hide()中的每一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆