【问题标题】:Toggle between two divs, reset once opened在两个 div 之间切换,打开后重置
【发布时间】:2018-11-20 22:58:52
【问题描述】:

我有两个按钮,分别称为“关于我”和“项目”。我已经做到了,无论我点击哪个按钮,它们都有自己的内容显示“在同一个地方”。 (在 div 之间切换)

我还实现了您可以在单击其中一个按钮后在显示或不显示内容之间切换。但是,我的问题是我希望能够在“关于我”和“项目”之间切换,并且总是显示它们的内容(当我在这两者之间切换时),并且只有当我在同一个按钮上单击两次时才隐藏它们的内容

我认为问题出在我的切换功能上,所以我将代码粘贴到这里。希望有人能理解我的问题。

function toggleAbout() {
var showAbout = document.getElementById("aboutBtn");
var hideAbout = document.getElementById("aboutDiv");

showAbout.addEventListener('click', function () {
    hideAbout.classList.toggle("show");
  });
}

function toggleproject() {
var showproject = document.getElementById("projectBtn");
var hideproject = document.getElementById("projectDiv");

showproject.addEventListener('click', function () {
     hideproject.classList.toggle("show");
   });
 }

函数调用在 index.html 中进行

<button id="aboutmeBtn" class="btn" onclick="toggleAbout()"> About Me   </button>
<button id="ProjectBtn" class="btn" onclick="toggleproject()"> Project</button>

【问题讨论】:

  • 您能否提供您的电话toggleAbouttoggleproject 来自哪里?我猜你在点击按钮时会打电话给他们,但我只是想确定一下。
  • 是的,我在我的 html 文件中调用它们。我已将它们包含在帖子中。

标签: javascript


【解决方案1】:

我认为@JO3-W3B-D3V 的解决方案有点复杂,所以我会尝试以不同的方式回答。

我认为您的问题是您在单击按钮时运行addEventListeners,但它应该在页面加载时运行。

我还取出了切换开关,而是手动更改了显示或隐藏的类。

试试这个:

var showAbout   = document.getElementById("aboutBtn");
var hideAbout   = document.getElementById("aboutDiv");
var showproject = document.getElementById("projectBtn");
var hideproject = document.getElementById("projectDiv");

showAbout.addEventListener('click', function () {
    if (hideAbout.classList == 'hide') {
        hideAbout.classList = 'show';
        hideproject.classList = 'hide';
    } else {
        hideAbout.classList = 'hide';
        hideproject.classList = 'hide';
    }
});

showproject.addEventListener('click', function () {
    if (hideproject.classList == 'hide') {
        hideproject.classList = 'show';
        hideAbout.classList = 'hide';
    } else {
        hideproject.classList = 'hide';
        hideAbout.classList = 'hide';
    }
});
.show {
  display: block;
}

.hide {
  display: none;
}

#yourApp div {
  height: 120px;
  width: 120px;
}

#aboutDiv {
  background-color: pink;
}

#projectDiv {
  background-color: dodgerblue;
}

#aboutBtn {
  background-color: pink
}

#projectBtn {
  background-color: dodgerblue
}
<div id="yourApp">
  <div id='aboutDiv' class='show'>About</div>
  <div id='projectDiv' class='hide'>Project</div>
  
  <button id="aboutBtn">About</button>
  <button id="projectBtn">Project</button>
</div>

【讨论】:

  • 我同意,但我不能 100% 确定他想要什么……所以我想嘿嘿,尽可能多地给他!?
  • 是的,很抱歉我没有正确阅读问题,现在就编辑它
  • 嘿,Mike,别担心,我的意思是我知道我的解决方案针对手头的问题设计得有些过度,但至少它可以在一定程度上扩展,哈哈。
  • 这正是我所需要的。谢谢!!
【解决方案2】:

这是你所追求的有点吗? ...

!function () {
  var aboutBtn = document.getElementById("aboutBtn");
  var projectBtn = document.getElementById("projectBtn");
  var aboutPage = document.getElementById("aboutDiv");
  var projectPage = document.getElementById("projectDiv");

  // The class name for visible elements.
  var showState = 'show';

  // The class name for hidden elements.
  var hideState = 'hide';

  // Boolean value to state if you wish to
  // automatically toggle between
  // the two elements.
  var toggle = false;

  // Forces the other element to be hidden.
  var hideOther = true;


  // Simply states if the provided element
  // is visible or not.
  var isVisible = function (el) {
    return el.className.toLowerCase().indexOf(showState) >= 0;
  };

  // Simple method to swap the visibility
  // state.
  var swapState = function (el, oel) {
    if (isVisible(el)) el.className = hideState;
    else el.className = showState;

    if (oel != null)
      if (el.className === showState) oel.className = hideState;
      else oel.className = showState;
  };

  var controller = function (el) {
    var me, other;

    // Simply workout which button has been pressed.
    if (el.getAttribute('id') === projectBtn.getAttribute('id')) {
      me = projectPage;
      other = aboutPage;
    } else {
      me = aboutPage;
      other = projectPage;
    }

    // If toggle is false.
    if (!toggle) swapState(me);
    else swapState(me, other);

    // Both wouldn't really work together,
    // at least to my knowledge.
    if (hideOther && !toggle) other.className = hideState;
  };

  // Simply bind the event handler.
  aboutBtn.addEventListener('click', function () { controller(aboutBtn); });
  projectBtn.addEventListener('click', function () { controller(projectBtn); });
}();
.show {
  display: block;
}

.hide {
  display: none;
}

#yourApp div {
  height: 100px;
  width: 100px;
}

#aboutDiv {
  background: pink;
}

#projectDiv {
  background: dodgerblue;
}
<div id="yourApp">
  <div id='aboutDiv' class='show'></div>
  <div id='projectDiv' class='hide'></div>
  
  <button id="aboutBtn">About</button>
  <button id="projectBtn">Project</button>
</div>

【讨论】:

  • 两个按钮之间的切换是正确的,但我也希望它能够(在您的示例中)在我单击同一个按钮时在颜色之间切换,例如:第一次单击 -打开 div 第二次点击 - 关闭 div 第三次点击 - 打开 div
  • 在这种情况下,您只需执行我上面所做的操作即可。在我提供的示例中,您只需将 class-name 属性设置为“隐藏”。
  • 我会试试的!谢谢!!
  • 希望我最近的更新尽可能有帮助,我什至在那个版本中包含了 cmets。
猜你喜欢
  • 1970-01-01
  • 2020-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多