这可以转换为适用于多个(20+)实例吗?
我将此代码用于常见问题解答。单击该图片会打开该框,单击该图片也会关闭该框。每次点击都会切换 img。到目前为止,我还无法将其转换为适用于超过 1 个部分。我对jquery或javascript一无所知,但了解一些编程概念。我已经尝试了 4 个小时但没有成功。我需要你的帮助!
$(document).ready(function() {
$('#expandcontract').toggle(
function(){ // you can add as much here as you'd like
$('#box').show('slow');
$('#imgArrows').attr("src","images/up.png");
}, function() { // same here
$('#box').hide('slow');
$('#imgArrows').attr("src","images/down.png");
});
});
I'm using this code for a faq. You click on the img and it opens the box, clicking on the img also closes the box. The img switches on each click. So far I have not been able to convert this to work with more than 1 section. I know nothing about jquery or javascript, but understand some programming concepts. I have been trying for 4 hours with no luck. I need your help!
$(document).ready(function() {
$('#expandcontract').toggle(
function(){ // you can add as much here as you'd like
$('#box').show('slow');
$('#imgArrows').attr("src","images/up.png");
}, function() { // same here
$('#box').hide('slow');
$('#imgArrows').attr("src","images/down.png");
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将所有 Id 更改为类后,您可能需要如下所示的内容:
假设您的 HTML 类似于:
After changing all the Id's to classes, you probably want something like this:
Assuming that your HTML is something like:
ID 只能提供给一个元素。您需要分配元素类,然后检索具有适当类的所有元素并向它们添加切换处理程序。
IDs should only be provided to one element. You'll want to assign the elements class, then retrieve all the elements with the appropriate class and add a toggle handler to them.