首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当单击外部时可视地替换div

当单击外部时可视地替换div
EN

Stack Overflow用户
提问于 2022-06-08 11:29:51
回答 1查看 75关注 0票数 0

代码语言:javascript
复制
function replace( hide, show ) {
                document.getElementById(hide).style.display="none";
                document.getElementById(show).style.display="flex";
            }
代码语言:javascript
复制
@import url('https://fonts.googleapis.com/css2?family=Allerta+Stencil&family=Bebas+Neue&family=Inter:wght@100;200;300;400;600;700;800&family=Roboto:wght@300;400;700&family=VT323&display=swap');


    body {
        margin: 0;
        padding: 0;
    }

    .all_guidebook {
        width: 100%;
        position: absolute;
        min-height: 1000px;
        height: auto;
        display: flex;
        flex-direction: column;
        align-items: center;
    }

    .title_guidebook {
        position: relative;
        top: 50px;
        justify-content: center;
    }

    .disposition_guidebook {
        width: 1100px;
        display: flex;
        position: relative;
        align-items: flex-start;
        top: 55px;
    }

    .navigation {
        width: 286px;
        background: var(--bleu);
        position: relative;
        height: auto;
        padding-bottom: 30px;
        border-radius: 35px;
        margin-right: 15px;
    }

    .navigation .all_links {
        position: relative;
        top: 15px;
    }

    .navigation h1 {
        color: white;
        font: 45px 'Bebas Neue';
        margin: 0;
        text-align: center;
        position: relative;
        top: 15px;
    }

    .navigation h2 {
        color: white;
        font: 34px 'Bebas Neue';
        position: relative;
        background: var(--fushia);
        display: flex;
        border-radius: 15px;
        height: 45px;
        padding-top: 2px;
        margin-bottom: 26px !important;
        width: 247px;
        box-sizing: border-box;
        top: 20px;
        margin: auto;
        justify-content: center;
    }

    .navigation .menu_deroul, #regl {
    display: flex;
    background: blue;
    width: 300px;
    }
    #reglhover {
    display: flex;
    background: blue;
    width: 300px;
    }
#reglhover img, #staffcredshover img, #ctxhover img, #grphover img, #pvrhover img, #syst1hover img, #syst2hover img, #pihover img, #exphover img, #foirehover img {
    transform: rotate(135deg);
    top: 5px;
    position: relative;
    left: 7px;
}
.navigation .menu_deroul img {
    float: left;
    left: 7px;
    position: relative;
    cursor: pointer;
}

.navigation span {
    color: var(--fushiapp);
    margin-right: 3px;
    font: 20px 'Bebas Neue';
}

.navigation h3 {
    color: var(--white);
    font: 20px 'Bebas Neue';
}
.wrap_deroul {
    display: flex;
    flex-direction: column;
    position: relative;
    top: -18px;
    align-items: flex-end;
}
.ancres_deroul {
    list-style: none;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    height: 30px;
    padding: 0;
    color: white;
    top: -16px;
    font-weight: 200!important;
    right: 16px;
    font: 16px 'Bebas Neue';
    position: relative;
}
.ancres_deroul ul {
    margin: 0;
    display: flex;
    cursor: pointer;
    list-style: none;
    align-items: flex-end;
    flex-direction: column;
}
.ancres_deroul li {transition: .5s;}
.ancres_deroul li a {
    text-decoration: none!important;
    color: black;
}
.ancres_deroul li:hover {
    letter-spacing: .5px;
    transition: .5s;
}
代码语言:javascript
复制
<div class="menu_deroul" id="regl" style="display:flex" >
                    <img src="https://i.ibb.co/cvhdX78/image.png" height="21" width="21" onclick="replace('regl','reglhover')" />
                    <div class="titre_deroul">
                        <span>001.</span>
                        <h3 class="tablinks" onclick="openCity(event, 'reglement')" id="defaultOpen">Règlement</h3>
                    </div>
                </div>
                <div id="reglhover" style="display:none">
                    <img src="https://i.ibb.co/cvhdX78/image.png" height="21" width="21" style="cursor:pointer; transform: rotate(135deg); transition: .5s; transition-duration: 2s;" onclick="replace('reglhover','regl')" />
                    <div class="wrap_deroul">
                        <div class="titre_deroul">
                            <span>001.</span>
                            <h3 class="tablinks" onclick="openCity(event, 'reglement')">Règlement</h3>
                        </div>
                        <div class="ancres_deroul">
                            <ul>
                                <li><a onclick="scrollWin()">Inscription & RP</a></li>
                                <li style="margin-top: -5px;"><a href="reglement.html#discord" target="myIframe">Discord</a></li>
                            </ul>
                        </div>
                    </div>
                </div>

我有这段代码,它在单击按钮时将div替换为另一个div。

当单击div外部时,我希望它关闭显示的div。例如,如果显示div "reglhover“,我希望在单击"reglhover”div之外时,将其替换为“隐藏”状态(由“regl”替换)。我希望它对每一件事都有效(因为我用标签).但我不知道该怎么做。有人能帮我吗?

EN

回答 1

Stack Overflow用户

发布于 2022-06-08 13:01:34

试一试

代码语言:javascript
复制
const regl = document.getElementById("regl")
const reglhover = document.getElementById("reglhover")

document.addEventListener("click", (e) => {
    if (reglhover.style.display === "flex" && e.target.id != "reglhover") {
        reglhover.style.display = "none"
        regl.style.display = "flex"

    }
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72544946

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档