如何切换类,以便单击主体将关闭下拉菜单并更改剩余项目(jquery)的背景?

发布于 2024-10-13 07:05:19 字数 741 浏览 3 评论 0原文

有一个下拉菜单,由展开/折叠按钮 (#client-select) 和下拉菜单 (.client-select-dd) 组成。菜单本身工作得很好,只是我希望如果用户单击菜单之外的任何位置,而不是专门再次单击按钮,它也可以折叠。

最初菜单是折叠的,因此第一次单击会更改菜单按钮的背景(#client-select)并使下拉选项可见(.client-select-dd)。第一次单击还会向主体添加一个类(.client-deactivate)。再次单击 (#client-select) 会折叠菜单并恢复按钮图像背景。

在我看来,在菜单打开时(并且具有“.client-deactivate”类)单击正文应该折叠菜单并删除 .client-deactivate,但单击正文绝对不会执行任何操作。感谢大家提供的任何帮助。

$("#client-select").click (function() {
  $("body").toggleClass("client-deactivate");
  $(".client-select-close").toggleClass("client-select-open");
  $(".client-select-dd").toggle();
});

$(".client-deactivate").click (function() {
  $(".client-select-open").toggleClass("client-select-close");
  $(".client-select-dd").toggle();
});

There is a menu dropdown comprised of the expand/collapse button (#client-select) and the dropdown menu (.client-select-dd). The menu itself works just fine except that I want it to also be collapsible if the user clicks anywhere outside the menu instead of specifically having to click the button again.

Initially the menu is collapsed, so the first click changes the background on the menu button(#client-select) and makes the dropdown options visible(.client-select-dd). This first click also adds a class to the body(.client-deactivate). Clicking on (#client-select) again collapses the menu and restores the button image-background.

In my mind, clicking on the body while the menu is open (and has the class ".client-deactivate") should collapse the menu and remove .client-deactivate, but clicking on the body does absolutely nothing. Thanks for any help all of you can provide.

$("#client-select").click (function() {
  $("body").toggleClass("client-deactivate");
  $(".client-select-close").toggleClass("client-select-open");
  $(".client-select-dd").toggle();
});

$(".client-deactivate").click (function() {
  $(".client-select-open").toggleClass("client-select-close");
  $(".client-select-dd").toggle();
});

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

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

发布评论

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

评论(2

给妤﹃绝世温柔 2024-10-20 07:05:19

想通了:

$("#client-select").live('click', function() {
$("#client-select").addClass("client-select-open");
$("#client-select").removeClass("client-select-close");
$(".client-select-dd").css("显示", "阻止");
$("body").addClass("客户端停用");
});

$(".client-deactivate").live('click', function() {
  $("#client-select").removeClass("client-select-open");
  $("#client-select").addClass("client-select-close");
  $(".client-select-dd").css("display", "none");
  $("body").removeClass("client-deactivate");
});

Figured it out:

$("#client-select").live ('click', function() {
$("#client-select").addClass("client-select-open");
$("#client-select").removeClass("client-select-close");
$(".client-select-dd").css("display", "block");
$("body").addClass("client-deactivate");
});

$(".client-deactivate").live('click', function() {
  $("#client-select").removeClass("client-select-open");
  $("#client-select").addClass("client-select-close");
  $(".client-select-dd").css("display", "none");
  $("body").removeClass("client-deactivate");
});
a√萤火虫的光℡ 2024-10-20 07:05:19

这可能是因为当浏览器第一次读取点击处理程序时,body 标记上没有“.client-deactivate”类。另外,当您单击正文时,您不想切换任何内容,因为如果菜单折叠并且您单击正文,它会展开菜单。我假设您只希望主体单击处理程序关闭菜单(如果菜单已打开)。

尝试将第二个单击处理程序更改为:

$("body").click(function() {
  if($("body").hasClass("client-deactivate") {
    $(".client-select-open").toggleClass("client-select-close");
    $(".client-select-dd").toggle();
    $("body").removeClass("client-deactivate");
  }
});

您还可以考虑用removeClass替换这些切换函数,只是为了确保单击主体不会展开菜单...就像另一个函数要折叠菜单但不折叠菜单一样将“client-deactivate”类删除到主体,然后单击主体仍将具有类名称并运行上面的代码,将菜单切换到打开位置。

This is probably because when the click handler is first read by the browser, there is no ".client-deactivate" class on the body tag. Also, you don't want to toggle anything when you click on the body, because then if the menu is collapsed and you clicked on the body, it would expand the menu. I assume that you only want the body click handler to close the menu if it's already open.

Try changing the second click handler to:

$("body").click(function() {
  if($("body").hasClass("client-deactivate") {
    $(".client-select-open").toggleClass("client-select-close");
    $(".client-select-dd").toggle();
    $("body").removeClass("client-deactivate");
  }
});

You might also consider replacing these toggle functions with removeClass, just to be sure that clicking on the body doesn't expand the menu... Like if another function were to collapse the menu but doesn't remove the "client-deactivate" class to the body, then clicking on the body would then still have the class name and run the above code, toggling the menu to the open position.

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