首页 > 解决方案 > 基本 Jquery 问题 - 隐藏元素

问题描述

对不起这个问题,但我似乎无法弄清楚。我试图在页面加载时隐藏“#postcodeConf”元素,但它就是不这样做!我将代码放在 JSfiddle 中,它按预期工作!

我究竟做错了什么?!

<html>
<head>
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<script>
$("#postcodeConf").hide();
</script>

</head>
<body>
        <input type="text" id="postcodeEntry">
        <button onclick="myFunction()">Search</button>
        <p id="postcodeConf">Postcode Ok</p>
</body>
</html>

标签: javascripthtmljquery

解决方案


    <input type="text" id="postcodeEntry">
     <button onclick="myFunction()">Search</button>
     <p id="postcodeConf">Postcode Ok</p>  

    <script>
     $("#postcodeConf").hide();
    </script>

按以下顺序添加,因为只有在渲染后才能捕获 dom 元素。


推荐阅读