首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用display属性操作元素

使用display属性操作元素
EN

Stack Overflow用户
提问于 2015-06-11 17:17:12
回答 1查看 40关注 0票数 0
代码语言:javascript
复制
<!DOCTYPE html>
<html>

<head>
    <style>
        #myDIV {
            width: 500px;
            height: 500px;
            background-color: lightblue;
            display: none;
        }

        #second {
            width: 500px;
            height: 500px;
            background-color: lightblue;
            display: none;
        }
    </style>
</head>

<body>

    <button onclick="myFunction()">Try it</button>
    <button onclick="myFunction2()">Try this now</button>
    <div id="myDIV">
        This is my DIV element.
    </div>
    <div id="second">
        This is my DIV2 element.
    </div>

    <script>
        function myFunction() {
            document.getElementById("myDIV").style.display = "block";
        }
    </script>

    <script>
        function myFunction2() {
            document.getElementById("second").style.display = "block";
        }
    </script>
</body>

</html>

因此,我有这样的代码,它将使div元素出现在单击按钮上。然而,我希望一旦我点击“第二”按钮,我希望"myDiv“消失,”第二“取代它的位置(确切的位置),反之亦然。

有什么帮助吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-11 17:21:29

代码语言:javascript
复制
<script>
    function myFunction() {
        document.getElementById("myDIV").style.display = "block";
        document.getElementById("second").style.display = "none";
    }
</script>

<script>
    function myFunction2() {
        document.getElementById("second").style.display = "block";
        document.getElementById("myDIV").style.display = "none";
    }
</script>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30787343

复制
相关文章

相似问题

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