javascript - 基本 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>
解决方案
<input type="text" id="postcodeEntry">
<button onclick="myFunction()">Search</button>
<p id="postcodeConf">Postcode Ok</p>
<script>
$("#postcodeConf").hide();
</script>
按以下顺序添加,因为只有在渲染后才能捕获 dom 元素。
推荐阅读
- groovy - 在 Gradle 的单元测试中使用 Spock 通配符的问题
- wpf - 使用 Azure SSO 的 MSIX 安装的 WPF 应用程序将无法启动
- windows - 用于列出无法更改密码的用户和密码过期设置为从不的用户的 Windows 命令
- c# - c# 无法重定向要求输入内容的消息
- machine-learning - 为严重不平衡的数据提供适当的无偏度量
- css - 用于嵌入函数的正确样式组件 taggedTemplate 语法
- c# - 无法使用 SMTP 服务器通过 Office 365 发送电子邮件(MFA 已关闭)
- javascript - 如果减少后的值不小于 0,则减少值 Prisma
- c++ - C++ 地图插入——替换现有元素
- python-3.x - 从网站中提取文本数据并存储它