简体   繁体   English

如何使用toggle_visibility隐藏div然后在单击时显示另一个div

[英]How to hide div then show another div on click using toggle_visibility

I want show div 1 on html load while hiding div 2, then using onclick I want to exchange their visibility like when clicking button hide div1 then show div2, then when clicking again, hide div2 then show div 1. Here is my code:我想在隐藏 div 2 的同时在 html 加载上显示 div 1,然后使用 onclick 我想交换它们的可见性,例如单击按钮隐藏 div1 然后显示 div2,然后再次单击时,隐藏 div2 然后显示 div 1。这是我的代码:

 function toggle_visibility(id) { var e = document.getElementById(id); if(e.style.display == 'none') e.style.display = 'block'; else e.style.display = 'none'; }
 <a href="#" onclick="toggle_visibility('foo');">Hide DIV 1 show DIV 2</a> <div id="foo"> This is DIV 1</div></div> <div id="foo"> This is DIV 2</div></div>

Initially set your div1 to display:none;最初将您的div1设置为display:none; and simply toggle them.并简单地切换它们。 I have used class targetElement just to get rid of common selector.我使用类targetElement只是为了摆脱常见的选择器。 hidden class is used to use the default style display:none; hidden类用于使用默认style display:none; and to replace the inline-style .并替换inline-style .

I woul like to use button instead of <a href="#">Click here</a> .我想使用button而不是<a href="#">Click here</a> to do this simple replace a tag with <button type="button"> Button Name </button>要做到这一点,只需将标签替换为<button type="button"> Button Name </button>

 $('#toggleButton').on('click',function(){ $('.targetElement').toggleClass('hidden'); });
 .hidden{ display:none; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <a id="toggleButton" href="#">Click here</a> <div id="foo" class="targetElement hidden"> This is DIV 1</div></div> <div id="foo" class="targetElement"> This is DIV 2</div></div>

It is not good practice to have 2 elements with the same ID.具有相同 ID 的 2 个元素不是一个好习惯。 ID's should always be unique. ID 应该始终是唯一的。 Use classes instead.改用类。 To hide a div onload.在加载时隐藏 div。 Simply add the css to your html element in a style attribute.只需在style属性中将 css 添加到 html 元素。 You also had unecessary </div> tags that I removed.你也有不必要的</div>标签被我删除了。

Here is working code using JQuery (less code) :这是使用 JQuery 的工作代码(更少的代码):

 $("#toggle").on("click", function() { $(".isToggable").toggle(); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <a id="toggle" href="#" >Hide DIV 1 show DIV 2</a> <div class="isToggable"> This is DIV 1</div> <div class="isToggable" style="display:none"> This is DIV 2</div>

In the html, I added classes to elements that you want to be toggled.在 html 中,我向要切换的元素添加了类。 I also added an ID to the a tag.我还在a标签中添加了一个 ID。 In the JQuery I added an event listener onclick on that a tag using it's ID.在 JQuery 中,我添加了一个事件侦听器 onclick 使用它的 ID 标记a标签。 $(".isToggable").toggle(); takes all the elements with the class "isToggable" in the page toggles their visibility.获取页面中具有“isToggable”类的所有元素来切换它们的可见性。

With no parameters, the .toggle() method simply toggles the visibility of elements没有参数, .toggle() 方法只是切换元素的可见性

More information on toggle : JQuery Toggle有关切换的更多信息: JQuery 切换


Old answer (vanilla javascript)旧答案(香草javascript)

 function toggle_visibility(id) { var container = document.getElementById(id); for (var i = 0; i < container.children.length; i++) { let currentElem = container.children[i]; if (currentElem.style.display === "none") { currentElem.style.display = "block"; } else { currentElem.style.display = "none"; } } }
 <a href="#" onclick="toggle_visibility('container');">Hide DIV 1 show DIV 2</a> <div id="container"> <div> This is DIV 1</div> <div style="display:none"> This is DIV 2</div> </div>

How this works is you put your elements that you want to toggle the visibility inside a container and the toggle_visibility function will toggle all the visibilities of the elements inside.这是如何工作的,您将要切换可见性的元素放在容器内, toggle_visibility函数将切换内部元素的所有可见性。 That way if you decide to add more div's they will all be handled.这样,如果您决定添加更多 div,它们都将被处理。

If you have any questions on how this works.如果您对它的工作原理有任何疑问。 Don't hesitate to ask.不要犹豫,问。 Hope it helps !希望能帮助到你 !

The attributeid must be unique in a document.属性id在文档中必须是唯一的。 Use class instead.改用

First use CSS to hide the second div.首先使用 CSS 隐藏第二个 div。 Then use forEach() to hide/show div.然后使用forEach()隐藏/显示 div。 You should also avoid using inline event handler.您还应该避免使用内联事件处理程序。

Try the following way:尝试以下方式:

 document.querySelector('a').addEventListener('click',function(){ [].forEach.call(document.querySelectorAll('.foo'), function(el){ if(el.style.display == 'none') el.style.display = 'block'; else el.style.display = 'none'; }); });
 <a href="#">Hide DIV 1 show DIV 2</a> <div class="foo"> This is DIV 1</div></div> <div class="foo" style="display:none;"> This is DIV 2</div></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM