javascript - 当输入框以 HTML 为焦点时如何做某事
问题描述
我有一个 ID 为“searchbox”的搜索框——我试图根据用户是否关注搜索框来显示和隐藏一个名为“feed”的元素。
<script>
document.getElementById("searchbox").addEventListener("focus", myFunction);
function myFunction() {
document.getElementById("feed").style.display= "none";
}
</script>
上面的函数运行不正常(提要没有消失) - 所以如果我能得到一些关于如何更好地构造函数的指导,那就太好了
解决方案
feed
要切换blur
,除了focus
.
这是一个工作示例。确保你的script
标签在你的 DOM 元素之后:
<html>
<input id="searchbox" type="input" />
<div id="feed">feed</div>
<script>
const searchbox = document.getElementById("searchbox");
const feed = document.getElementById("feed");
searchbox.addEventListener("focus", () => feed.style.display = "none");
searchbox.addEventListener("blur", () => feed.style.display = "block");
</script>
</html>
推荐阅读
- c# - WPF 并行运行多个任务(selenium webdrivers)无法正常工作
- mongodb - 为字母数字字段创建 mongo db 索引的正确方法
- java - 向 API 传递多个参数
- flutter - 如何修复 Flutter 错误消息:颜色不是类型?
- reactjs - 使用 Lambda 函数 + API 网关将图像从 ReactJS 上传到 AWS S3
- centos - CyberPanel 网络邮件 RainLoop 不发送/接收
- python - 更新第一组中所有行的列
- python - 在 Python 中获得漂亮的操作系统名称
- c++ - Int 数组与 Char 数组
- python - 对于循环加法器问题