尝试使用 Jquery 切换多个 DIV
我一直在努力让这个功能发挥作用一段时间了。我有 11 个默认隐藏的不同 div。每个都有一个“触发”按钮,可以让它们弹出。我能够为每个特定的 div 使用更长的一系列函数来完成此操作(最终大约有 175 行代码!)。我想将其压缩为一个可以执行相同操作的函数,即:单击触发器 1 - div 1 打开/再次单击触发器 1,div 1 关闭等等。
这是我的脚本,目前
$(document).ready(function() {
$("[class^='ShowVehicles']").toggle(function() {
alert("click happened");
$("[class^='HiddenVehicles']").slideDown(1000);
},
function () {
$("[class^='HiddenVehicles']").slideUp(1000);
});
});
正在打开所有“HiddenVehicles”div,我需要使其更加具体。我对 jquery 相当陌生,所以我几乎没有什么想法。预先感谢各位!
编辑 - 感谢所有帮助人员,这是我想出的解决方案:
$(document).ready(function() {
$("div.WrapIt").toggle(
function(){ $(".HiddenVehicles", this).slideDown(1000);
$("img.imgSwap" , this).attr("src","assets/images/SelectBySize/SelectBySize_HideAll.gif");
},
function(){ $(".HiddenVehicles", this).slideUp(1000);
$("img.imgSwap" , this).attr("src","assets/images/SelectBySize/SelectBySize_ShowAll.gif")
}
);
$(".ShowEveryThing").toggle(
function() { $(".WrapIt .HiddenVehicles").slideDown(1000);
$("img.imgSwap").attr("src","assets/images/SelectBySize/SelectBySize_HideAll.gif");
$("img.buttonSwap").attr("src","assets/images/SelectBySize/SelectBySize_HideBtn.gif");
},
function(){ $(".WrapIt .HiddenVehicles").slideUp(1000);
$("img.imgSwap").attr("src","assets/images/SelectBySize/SelectBySize_ShowAll.gif");
$("img.buttonSwap").attr("src","assets/images/SelectBySize/SelectBySize_ShowBtn.gif");
});
});
我创建了一个包装器 div,并在单击该 div 时触发了该函数。我还包括一些图像交换和显示所有功能。
I have been trying to get this function to work for awhile now. I have 11 different divs that are hidden by default. Each have a "trigger" button that is supposed to get them to pop open. I was able to do this with a much longer series of functions for each specific div (which ended up being about 175 lines of code!). I wanted to condense it down into a single function that would do the same thing ie: trigger 1 is clicked - div 1 opens/ trigger 1 is clicked again, div 1 closes etc, etc.
Here is my script as of now
$(document).ready(function() {
$("[class^='ShowVehicles']").toggle(function() {
alert("click happened");
$("[class^='HiddenVehicles']").slideDown(1000);
},
function () {
$("[class^='HiddenVehicles']").slideUp(1000);
});
});
It's currently opening all "HiddenVehicles" divs and I need to make it more specific. I'm fairly new to jquery so I have pretty much run out of ideas. Thanks in advance guys!
Edit - Thanks for all the help guys, here's the solution I came up with:
$(document).ready(function() {
$("div.WrapIt").toggle(
function(){ $(".HiddenVehicles", this).slideDown(1000);
$("img.imgSwap" , this).attr("src","assets/images/SelectBySize/SelectBySize_HideAll.gif");
},
function(){ $(".HiddenVehicles", this).slideUp(1000);
$("img.imgSwap" , this).attr("src","assets/images/SelectBySize/SelectBySize_ShowAll.gif")
}
);
$(".ShowEveryThing").toggle(
function() { $(".WrapIt .HiddenVehicles").slideDown(1000);
$("img.imgSwap").attr("src","assets/images/SelectBySize/SelectBySize_HideAll.gif");
$("img.buttonSwap").attr("src","assets/images/SelectBySize/SelectBySize_HideBtn.gif");
},
function(){ $(".WrapIt .HiddenVehicles").slideUp(1000);
$("img.imgSwap").attr("src","assets/images/SelectBySize/SelectBySize_ShowAll.gif");
$("img.buttonSwap").attr("src","assets/images/SelectBySize/SelectBySize_ShowBtn.gif");
});
});
I created a wrapper div and had the function fire when that div was clicked. I also included some image swapping and a show all function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要以某种方式将显示/隐藏按钮绑定到它们将执行操作的各自的 div 上。
您可以通过将它们封装在单个
元素中或为它们提供通用 ID 来识别哪个按钮控制哪个 div 来实现此目的。
下面是
示例:
jQuery 功能如下:
JS 代码很大程度上取决于您的 HTML 结构,因此如果您决定使其显着不同,则某些 jQuery 选择器将需要待调整
You need to somehowe tie the show/hide buttons to their respective divs that they will act on.
You can do this by encapsulating them inside a single
<LI>
element or giving them common IDs to identify which button controls which div.Here's the
<LI>
example:And the jQuery functionality would be:
The JS code greatly depends on what your HTML structure is so if you decide to make it significantly different then some of the jQuery selectors will need to be adjusted
试试这个:
Try this:
您想要做的是从您定义的函数中获取对特定
的引用。在定义的函数中,您应该能够使用 $(this) 获取对按钮的引用。根据您的格式,您现在应该能够得到
Say your markup is:
您现在可以将代码更改为如下所示:
这里的关键是对
siblings()
的调用,这将返回按钮“旁边”的所有元素(然后按 HiddenVehicles 类过滤),在本例中就是您的。然而,根据它们彼此定位的方式,您可能需要使用其他 jQuery 遍历函数 之一。
What you would want to do is get a reference to a specific
<div>
from within the function you've defined. Within the defined functions you should be able to get a reference to the button using $(this). Depending on your formatting you should now be able to get theSay your markup is:
You could now change your code to something like the following:
The key here is the call to
siblings()
, this will return all the elements that are "next" to the button (and then filter by class HiddenVehicles), which in this case is just your<div>
. Depending on how they are positioned to each other you may however need to use one of the other jQuery traversing functions.