jQuery:切换 DIV 时切换图像

发布于 2024-08-25 03:36:32 字数 398 浏览 4 评论 0原文

我正在尝试编写一些 jQuery 来切换 DIV 打开和关闭,并且它正在工作,但我想要一个箭头,如果 DIV 展开,则箭头将更改为“向下”,如果 DIV 折叠,则箭头将更改为“向右”。

到目前为止我所拥有的:

  $('.toggleHead').click(function() {
   $('#arrow').attr('src', 'images/icons/down.png');
   $(this).next().toggle();
   return false;
  }).next().hide();

我会将另一个 $('#arrow').attr('src', 'images/icons/right.png'); 放在哪里?

谢谢!

I'm trying to write some jQuery that will toggle DIVs open and closed, and it's working, but I want an arrow that will change to "down" if the DIV is expanded, and "right" if the DIV is collapsed.

What I have so far:

  $('.toggleHead').click(function() {
   $('#arrow').attr('src', 'images/icons/down.png');
   $(this).next().toggle();
   return false;
  }).next().hide();

Where would I put the other $('#arrow').attr('src', 'images/icons/right.png');?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

热鲨 2024-09-01 03:36:32

在 #arrow - 您可以使用 .toggleClass('open') 它将添加/删除 'open' 类。

然后在 CSS 中,您可以为 .open 设置背景图像,它将覆盖“向下”箭头。

我有类似的东西

$('.accordion-multi h2').click(function() {
    $(this).next().slideToggle('normal');
    $(this).toggleClass('open');
    return false;
}).next().hide();

On #arrow - you can use .toggleClass('open') which will add/remove the 'open' class.

Then in your CSS, you can set a background image for .open which will override the 'down' arrow.

I've got something like

$('.accordion-multi h2').click(function() {
    $(this).next().slideToggle('normal');
    $(this).toggleClass('open');
    return false;
}).next().hide();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文