[英]Show / hide div with two buttons
I have two buttons createChatWindow
and openSettings
. 我有两个按钮
createChatWindow
和openSettings
。 Each button displays a div. 每个按钮显示一个div。 I want the div to be hidden when clicked outside of it.
我希望在单击div时将其隐藏。 My problem is that when I click elsewhere the div is closed but the other one is displayed.
我的问题是,当我单击其他位置时,div已关闭,但显示了另一个。
Here is my html: 这是我的html:
<div class="chat-threads" id="chat-threads">
<button class="top-bar-btn create"
(click)="createChatWindow()">
<i class="fa fa-pencil-square-o"></i>
</button>
<button class="top-bar-btn settings"
(click)="openSettings()">
<i class="fa fa-cog"></i>
</button>
<div id="outside" (click)="hide()"></div>
</div>
And my .ts : 还有我的.ts:
hide(): void {
document.getElementById('outside').style.display = 'none';
document.getElementById('chat-threads').classList.toggle('display-settings');
document.getElementById('chat-threads').classList.toggle('display-new-chat-window');
}
openSettings(): void {
document.getElementById('outside').style.display = 'block';
document.getElementById('chat-threads').classList.toggle('display-settings');
}
createChatWindow(): void {
document.getElementById('outside').style.display = 'block';
document.getElementById('chat-threads').classList.toggle('display-new-chat-window');
}
When you click outside, so in the hide()
function, don't use toggle
, use remove
: 当您在外部单击时,在
hide()
函数中,请勿使用toggle
,而应使用remove
:
hide(): void {
document.getElementById('outside').style.display = 'none';
document.getElementById('chat-threads').classList.remove('display-settings');
document.getElementById('chat-threads').classList.remove('display-new-chat-window');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.