admin管理员组文章数量:1317906
<body>
<div id="e1">Element X1</div>
<div id="e2">Element 2X</div>
<div id="e3">Element X3</div>
<a href="/" id="e3">Hide</a>
</body>
How can i hide the entire body
And only show #e2
when i click on #hide
, But if i clicked anywhere else out of #e2
again, the hide effect will stop and return to normal.
<body>
<div id="e1">Element X1</div>
<div id="e2">Element 2X</div>
<div id="e3">Element X3</div>
<a href="/" id="e3">Hide</a>
</body>
How can i hide the entire body
And only show #e2
when i click on #hide
, But if i clicked anywhere else out of #e2
again, the hide effect will stop and return to normal.
- First of all, never never use an id twice... use #e3button or something for the link - you cant hide the body itself because #e3 is a child of body. What you can do is hide all divs inside body except e3 - have you got just one link and hide this link always the same id's except same / previously defined one ? – Marco Commented Apr 18, 2017 at 19:09
- Could that might help you? stackoverflow./questions/13239331/hide-all-divs-except-one – Marco Commented Apr 18, 2017 at 19:16
3 Answers
Reset to default 4Something like this? NB: make sure to give your hide link a unique ID.
Showing/hiding works well with jQuery show
and hide
methods, but since you wanted the elements to stay in their place, it is more suitable to use the visibility
style attribute:
$('#hide').click(function () {
// hide all in body except #e2, and #e2's parents.
$('body *').not($('#e2').parents().addBack()).css({visibility: 'hidden'});
return false; // cancel bubbling and default hyperlink effect.
});
$('#e2').click(function () { // click on #e2
return false; // cancel bubbling -- ignore click.
})
$(document).click(function (e) { // click on document
$('body *').css({visibility: 'visible'}); // show all in body.
});
div { border: 1px solid}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="e1">Element X1</div>
<div id="e2">Element 2X</div>
<div id="e3">Element X3</div>
<a href="/" id="hide">Hide</a>
Be aware that these div
elements stretch across horizontally, so a click to the right of the text "Element 2X" will still be on #e2.
Something like this:
// Get reference to the hyperlink
var hideElem = document.getElementById("e4");
// Set up click event handler for link
hideElem.addEventListener("click", function(e){
var elems = document.querySelectorAll("body *:not(#e2)");
Array.prototype.slice.call(elems).forEach(function(value){
value.classList.add("hide");
});
e.stopPropagation();
});
// Set up click event handler for document
document.addEventListener("click", function(){
var elems = document.querySelectorAll("body *");
Array.prototype.slice.call(elems).forEach(function(value){
value.classList.remove("hide");
});
});
.hide { display:none; }
<div id="e1">Element X1</div>
<div id="e2">Element 2X</div>
<div id="e3">Element X3</div>
<a href="#" id="e4">Hide</a>
$('body').click(function(evt){
if(!$(evt.target).is('#e2')) {
//If not e2 is clicked then restore the state back of page by removing a specific class
}
});
You will need help of css class .hide {display:none;} and add and remove this class when e2 is clicked and remove this class when body is clicked but not e2 as provided above
本文标签: javascriptHide all elements except oneAnd show all elements againStack Overflow
版权声明:本文标题:javascript - Hide all elements except one, And show all elements again - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742031651a2416552.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论