点击显示按钮,没有显示内容,求解答

img

img

你这个传入的this指向的是window,按楼上那样写是对的,还有用vscode或者hbuilderx开发吧,你的开发工具太老了


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #test {
            display: none;
        }
    </style>
</head>

<body>
    <input type="button" onclick="show()" value="点击显示">
    <input type="button" onclick="hide()" value="点击隐藏">
    <span id="test">这里是隐藏的内容</span>

    <script>
        function show() {
            let obj = document.getElementById("test");
            obj.style.display = "block";
        }

        function hide() {
            let obj = document.getElementById("test");
            obj.style.display = "none";
        }
    </script>

</body>

</html>

你只是定义了两个function,没有调用呀
由于文档加载 script 一般要放在HTML文档里面body的后面
或者在前面加Window.onload=function(){这里写你的script代码}