简体   繁体   English

document.querySelector() 不起作用

[英]document.querySelector() does not working

Is there any dependency problem?有没有依赖问题? because I am using the only pure javascript there is no jquery or anything.因为我使用的是唯一的纯 javascript,没有 jquery 或任何东西。 Using node.js使用 node.js
Here is my code这是我的代码

 window.onload()=function(){ var left=document.querySelector('.left'); var right=document.querySelector(".right"); var container=document.querySelector(".container"); console.log(left); left.addEventListener('mouseenter',()=>{ container.classList.add('hover-left'); }) left.addEventListener('mouseleave', () => { container.classList.remove('hover-left'); }) right.addEventListener('mouseenter', () => { container.classList.add('hover-right'); }) right.addEventListener('mouseleave', () => { container.classList.remove('hover-right'); }) }
 <div class="container"> <div class="split left"> <h1>The Designer</h1> <a href="" class="button">Read more</a> </div> <div class="split right"> <h1>The Programmer</h1> <a href="" class="button">Read more</a> </div> </div>

I got this error on Chrome:我在 Chrome 上收到此错误:

Uncaught TypeError: window.onload is not a function

Removing the parentheses the error was gone.去掉括号,错误就消失了。

But i didn't get how your page should work.但我不明白你的页面应该如何工作。

Look for more information here:在此处查找更多信息:

https://stackoverflow.com/questions/29927638/window-onload-work-but-chrome-console-says-uncaught-typeerror-window-onload-is https://stackoverflow.com/questions/29927638/window-onload-work-but-chrome-console-says-uncaught-typeerror-window-onload-is

If you select your DOM Elements with the CSS-Selector '.'如果您使用 CSS-Selector '.' 选择 DOM 元素for classes you get a list of elements instead of a single element which you can directly manipulate.对于类,您会得到一个元素列表,而不是您可以直接操作的单个元素。

If left, right and container only appear once on your page, it's best to use ids:如果 left、right 和 container 在页面上只出现一次,最好使用 ids:

JS: JS:

window.onload=function(){
    var left=document.getElementById('left');
    var right=document.getElementById("right");

    var container=document.getElementById("container");   
    console.log(left);
    left.addEventListener('mouseenter',()=>{
        container.classList.add('hover-left');

    })
    left.addEventListener('mouseleave', () => {
        container.classList.remove('hover-left');
    })
    right.addEventListener('mouseenter', () => {
        container.classList.add('hover-right');
    })
    right.addEventListener('mouseleave', () => {
        container.classList.remove('hover-right');
    })
}

HTML: HTML:

<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>

Also as a comment above points out you mustn't use brackets when setting onload event.同样作为上面的评论指出,在设置 onload 事件时不能使用括号。

If you have any static JS files being loaded in the tag in your html requesting to please load the JS at the end of the html body.如果您的 html 中的标签中加载了任何静态 JS 文件,请在 html 正文的末尾加载 JS。 Document wont be ready until the JS is loaded hence the document will be null.文档在加载 JS 之前不会准备好,因此文档将为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM