简体   繁体   English

当我在IE11中显示其他内容时,如何通过单击其区域以外的区域隐藏当前内容?

[英]How to hide current content by clicking outside its area, and when I show another content in IE11?

Clicking on the button shows and hides the corresponding content. 单击按钮显示并隐藏相应的内容。

  function funC(id) { var el = document.getElementById(id); if(el.style.display == 'inline-block') el.style.display = ''; else el.style.display = 'inline-block'; } 
 button {margin:2px; outline:0; font-size:12px;} span {padding:2px; border:1px solid silver; font-size:12px;} .cb {display:none;} 
 <button onclick="funC('cnt1');">b1</button><span id="cnt1" class="cb">content b1...</span> <br /> <button onclick="funC('cnt2');">b2</button><span id="cnt2" class="cb">content b2...</span> <br /> <button onclick="funC('cnt3');">b3</button><span id="cnt3" class="cb">content b3...</span> 

fiddle example 小提琴的例子
1. But, how to hide content when clicking outside its area, and as with showing the next content, hide the previous one? 1.但是,如何在单击内容区域之外时隐藏内容,并且与显示下一个内容一样,隐藏上一个内容?
2. Is it possible to do the same without using id? 2.是否可以不使用id而做同样的事情?
Only pure javascript. 只有纯JavaScript。 Thank you. 谢谢。

This might not be a perfect solution but here is a proposition : 这可能不是一个完美的解决方案,但这是一个建议:

 function hideAllContent() { var elements = document.querySelectorAll(".cb"); for(var i =0, l = elements.length; i < l; i++) { var element = elements[i]; element.visible = false; element.style.display='none'; } } function funC(id, event) { // We need to stop the event to avoid bubling until the body event.stopPropagation(); // let's hide others before displaying the new one hideAllContent(); var el = document.getElementById(id); if(el.visible) { el.visible = false; el.style.display = 'none'; } else { el.visible = true; el.style.display = 'inline-block'; } } document.body.onclick = function(event) { if (!event.target.classList.contains('cb')) { hideAllContent(); } } 
 button {margin:2px; outline:0; font-size:12px;} span {padding:2px; border:1px solid silver; font-size:12px;} .cb {display:none;} 
 <button onclick="funC('cnt1', event);">b1</button><span id="cnt1" class="cb">content b1...</span> <br /> <button onclick="funC('cnt2', event);">b2</button><span id="cnt2" class="cb">content b2...</span> <br /> <button onclick="funC('cnt3', event);">b3</button><span id="cnt3" class="cb">content b3...</span> 

About avoiding ids, you could use the target property on click event and find the sibling node or something like that or use a querySelector. 关于避免ID,您可以在click事件上使用target属性,然后找到同级节点或类似的节点,或者使用querySelector。 But ids are safe and fine i would say. 但是身份证很安全,我会说。

  • No inline on-clicks attached. 没有附加的内联点击。
  • No IDs use. 不使用ID。
  • Used backward-compatible syntax for IE 11. 对IE 11使用了向后兼容的语法。

 // JavaScript // get all button and span tags var btns = document.querySelectorAll('button'); var otherSpans = document.querySelectorAll('span'); // Detect all clicks on the document document.addEventListener("click", function(event) { const spanElems = document.querySelectorAll('span'); const spanElemsArray = Array.prototype.slice.call(spanElems); let matches = event.target.matches ? event.target.matches('button') : event.target.msMatchesSelector('button'); // If user clicks inside the element, do nothing if (matches) { return; } else { // If user clicks outside the element, hide it! spanElemsArray.forEach( function (spanElem) { spanElem.classList.remove("open"); }); } }); // convert buttons and spans variable objects to arrays const btnsArray = Array.prototype.slice.call(btns); const otherSpansArray = Array.prototype.slice.call(otherSpans); // loop through every button and assign a click to each one btnsArray.forEach( function (btn) { btn.addEventListener('click', spanFunc) }); // Pass the button clicked as a reference function spanFunc(){ openSpan(this); } // toggle the display class on the span next to the button using nextElementSibling method function openSpan(e) { e.nextElementSibling.classList.toggle("open"); // hide every other spans function otherSpanFunc() { otherSpansArray.forEach( function (otherSpan) { if (otherSpan !== e.nextElementSibling) { otherSpan.classList.remove('open'); } }); } otherSpanFunc(); } 
 /* css */ button {margin:2px; outline:0; font-size:12px;} span {padding:2px; border:1px solid silver; font-size:12px;} .cb {display:none;} .open {display:inline-block;} 
 <!-- HTML --> <button>b1</button><span class="cb">content b1...</span> <br /> <button>b2</button><span class="cb">content b2...</span> <br /> <button>b3</button><span class="cb">content b3...</span> 


jsFiddle: https://jsfiddle.net/ypofz4d5/55/ jsFiddle: https ://jsfiddle.net/ypofz4d5/55/

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

相关问题 使用jQuery在div外部单击时隐藏内容 - Hide content when clicking outside div with jquery 单击图像区域地图时如何显示div和CSS内容 - How to show div and css content when clicking on a image area map 当我在父级中使用transform translateY(0)时,可滚动div中的内容在IE11中闪烁 - Content inside scrollable div flickers in IE11 when I use transform translateY(0) in parent ie11 的聚合物含量问题 - Polymer content issue for ie11 IE11中的聚合物含量选择 - polymer content select in IE11 如何也可以通过在主要内容区域之外单击来关闭此弹出窗口? - How can I get this popup to close by clicking outside of the main content area as well? 通过单击外部内容区域来关闭GLightbox画廊 - Close GLightbox gallery by clicking outside content area IE11:在IE11中Content-Type = false无效 - IE11 : Content-Type = false in IE11 it doesn't work 禁用onclick事件 <td> 仅在单击文本时有效,但在单击td中的其他区域时无效(ie10,ie11 issue) - onclick event on disabled <td> only works when clicking text but not when clicking other area in td( ie10, ie11 issue) 如何在jqgrid的格式化区域之外显示单元格内容的正确工具提示 - How to show proper tooltip for cell content outside formatted area in jqgrid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM