javascript - 使用“document.querySelector”时出现“未捕获的 TypeError:非法调用”
问题描述
我有这个简单的两行 JS 代码,它不断出错,并出现“未捕获的类型错误:非法调用”。
const $ = document.querySelector;
$('#someElementId').display = 'none';
<p id="someElementId">The element</p>
解决方案
你错过了style
财产。
使用以下代码:
$('#someElementId').style.display='none
';
并且要修复,如@philIllegal Invocation
的评论中所述并在此答案中发布,您必须确保将您绑定到文档:querySelector
const $ = document.querySelector.bind(document);
这是一个最终工作的片段:
const $ = document.querySelector.bind( document );
$('#someElementId').style.display = 'none';
<p id="someElementId">The element</p>
推荐阅读
- reactjs - 不使用 Jest 和 Typescript 导入的导入
- hibernate - (springboot + hibernate + jparepository)错误 - 没有会话,当绑定到 GET
- java - GWT 上传小部件 - 将 POST 的查询字符串附加到 servlet 类
- cassandra - 最大 sstable 计数应该是多少才能改善 prod 集群上的读取延迟?
- network-programming - InetAddress.getLocalHost().getHostAddress() 和 InetAddress.getLocalHost().getAddress() 有什么区别?
- amazon-web-services - 将 Github 与 Amazon SQS 与 webhook 集成
- javascript - 以这种格式显示时间和日期
- javascript - 带有 ionic 3 的 allDbs 插件
- c# - C#中的看门狗通过互斥锁
- .net - 正则表达式在字符串中捕获小数