Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript document.querySelector()不工作_Javascript_Html - Fatal编程技术网

Javascript document.querySelector()不工作

Javascript document.querySelector()不工作,javascript,html,Javascript,Html,是否存在依赖性问题? 因为我使用的是唯一的纯javascript,所以没有jquery或任何东西。 使用node.js 这是我的密码 window.onload=函数{ var left=document.querySelector'.left'; var right=document.querySelector.right; var container=document.querySelector.container; 控制台。左; 左。addEventListener'mouseenter

是否存在依赖性问题? 因为我使用的是唯一的纯javascript,所以没有jquery或任何东西。 使用node.js 这是我的密码

window.onload=函数{ var left=document.querySelector'.left'; var right=document.querySelector.right; var container=document.querySelector.container; 控制台。左; 左。addEventListener'mouseenter',=>{ container.classList.add'hover-left'; } 左。addEventListener'mouseleave',=>{ container.classList.remove'hover-left'; } 对。添加了ventListener'mouseenter',=>{ container.classList.add'hover-right'; } 对。添加了一个列表“mouseleave”,=>{ container.classList.remove'hover-right'; } } 设计师 程序员
如果使用CSS选择器“.”为类选择DOM元素,则会得到元素列表,而不是可以直接操作的单个元素

如果左侧、右侧和容器仅在页面上出现一次,则最好使用ID:

JS:

HTML:


正如上面的一条评论所指出的,在设置onload事件时不能使用括号。

我在Chrome上遇到了这个错误:

Uncaught TypeError: window.onload is not a function
去掉括号,错误就消失了

但我不明白你的页面应该如何工作

请在此处查看更多信息:


如果有任何静态JS文件加载到html中的标记中,请求在html正文的末尾加载JS。在加载JS之前,文档不会准备就绪,因此文档将为空。

不工作是尽可能不准确的。。。请描述应该发生什么,改为发生什么,以及控制台中是否有任何错误,并在此处发布完整的错误消息。请尝试删除from window.onload=。。。仅使用window.onload=function…这里是未捕获的类型错误:window.onload不是functiondocument.querySelector.container可以,但我认为您可以在呈现dom之前执行脚本。尝试移动。。。块之前,你就不需要window.onload了。一旦你去掉了多余的,你的代码就可以完美地工作了。未捕获的TypeError:无法读取nullquerySelector的属性“addEventListener”只返回单个元素。此处querySelector返回null。在浏览器控制台中,其工作正常
<div id="container" class="container">
   <div class="split" id="left">
      <h1>The Designer</h1>
      <a href="" class="button">Read more</a>
   </div>
   <div class="split" id="right">
      <h1>The Programmer</h1>
      <a href="" class="button">Read more</a>
   </div>
</div>
Uncaught TypeError: window.onload is not a function