切换-隐藏和显示-设置为不同的元素/或替代

时间:2018-06-21 03:20:41

标签: javascript html toggle show-hide

我曾尝试使用this基本的w3schools代码来切换隐藏和显示选项,但最终陷入困境(我只管理html和CSS)。试图查看其他主题,但是如果功能变化太大,我会感到困惑。谢谢您的指导。

-我需要它在投资组合页面中工作,因此单击缩略图时,它将显示一个隐藏的div(覆盖整个页面),其中包含项目的完整说明和图片。 -我不知道如何设置“关闭按钮”,我只复制了一个按钮,只是更改了文字,不知道是否有更好的方法。

此外,我想知道是否有更好的方法来实现这一目标,而不是使用toogle函数(不是我不喜欢,但看起来很基本)

Fiddle

 <!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
#myDIV {
    width: 100%;
    height: 100%;
    padding: 50px 0;
    text-align: center;
    background-color: lightblue;
    margin-top: 20px;
}
</style>
</head>
<body>

<p>Click the "Try it" button to toggle between hiding and showing the DIV element:</p>

<button onclick="myFunction()">Try it</button>

<div id="myDIV">
This is my DIV element.
<br> <button onclick="myFunction()">close</button>

<p><b>Note:</b> The element will not take up any space when the display property set to "none".

</p>

<script>
document.getElementById('myDIV').style.display = "none"
function myFunction() {
    var x = document.getElementById("myDIV");
    if (x.style.display === "none") {
        x.style.display = "block";
        x.style.position = "absolute"
        x.style.top = "0";
    } else {
        x.style.display = "none";
    }
}

</script>

</body>
</html>

0 个答案:

没有答案