这可以转换为适用于多个(20+)实例吗?

发布于 2024-08-31 06:48:40 字数 461 浏览 3 评论 0原文

我将此代码用于常见问题解答。单击该图片会打开该框,单击该图片也会关闭该框。每次点击都会切换 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌能量女王 2024-09-07 06:48:40

将所有 Id 更改为类后,您可能需要如下所示的内容:

$(document).ready(function() {
    $('.expandcontract').toggle(
        function() {
            $('.box',this).show('slow');
            $('.imgArrows',this).attr("src","images/up.png");
        }, function() {
            $('.box',this).hide('slow');
            $('.imgArrows',this).attr("src","images/down.png");
        }
    );
});

假设您的 HTML 类似于:

<div class="expandcontract">
    <div class="box" />
    <div class="imgArrows" />
</div>
<div class="expandcontract">
    <div class="box" />
    <div class="imgArrows" />
</div>
<div class="expandcontract">
    <div class="box" />
    <div class="imgArrows" />
</div>

After changing all the Id's to classes, you probably want something like this:

$(document).ready(function() {
    $('.expandcontract').toggle(
        function() {
            $('.box',this).show('slow');
            $('.imgArrows',this).attr("src","images/up.png");
        }, function() {
            $('.box',this).hide('slow');
            $('.imgArrows',this).attr("src","images/down.png");
        }
    );
});

Assuming that your HTML is something like:

<div class="expandcontract">
    <div class="box" />
    <div class="imgArrows" />
</div>
<div class="expandcontract">
    <div class="box" />
    <div class="imgArrows" />
</div>
<div class="expandcontract">
    <div class="box" />
    <div class="imgArrows" />
</div>
Saygoodbye 2024-09-07 06:48:40

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文