如何在 jquery 中打开下一个按钮之前关闭打开的按钮?

How do I close the button which is open before the next one is being opend in jquery?

我有两个 div 用作按钮,但是当我激活第一个并单击另一个时,第一个打开 div 不会自行关闭,因此它们会重叠,所以你有手动关闭那些,这真的是不切实际的。 以下是功能:

$(function() {
    $('#profileButtonImg img').click(function() {
      $("#myProfileMain").stop(true).fadeToggle(250);
    });
});
$(function() {
    $('#optionsButtonImg').click(function() {
      $("#myOptionsMain").stop(true).fadeToggle(250);
    });
});

这里是点击打开 divs 的按钮:

<img id="profileButtonImg" src="pic1.jpg" /></div>
<div id="optionsButtonImg"></div>

以下是正在混合的 divs:

<div id="myProfileMain" style="display:none;"></div>
<div id="myOptionsMain" style="display:none;"></div>

改变这个

('#profileButtonImg img').click(

到这个

('#profileButtonImg').click(

第一行表示触发器是 div 中的一个 img,其 ID 为 profileButtonImg。但实际上 img 本身就有那个 Id

有很多比我下面的回答更好的方法:

$(function() {
    $('#profileButtonImg').click(function() {
      $("#myProfileMain").stop(true).fadeToggle(250);
      $("#myOptionsMain").stop(true).fadeOut(250);
    });
});
$(function() {
    $('#optionsButtonImg').click(function() {
      $("#myOptionsMain").stop(true).fadeToggle(250);
      $("#myProfileMain").stop(true).fadeOut(250);
    });
});