简体   繁体   English

控制台说元素为“ null”

[英]Console says element is “null”

I've started to create some kind of image slider but right at the beggining i've reached a problem 我已经开始创建某种图像滑块,但是刚开始时我遇到了一个问题

Console says that my img1 and others are "null".. 控制台说我的img1和其他人为“ null”。

Where's my mistake ? 我的错在哪里?

let img1 = document.querySelector('.images_first active');
let img2 = document.querySelector(".images_second");
let img3 = document.querySelector(".images_third");

const fw = document.querySelector(".test-button_forward");
const back = document.querySelector(".test-button_backwards");

let clicks = 0;
console.log(img1);


fw.addEventListener('click', function()
 {
  img1.classList.remove('active');
  img2.classlist.add("active");

  clicks++;

});

Full version is here 完整版本在这里

let img1 = document.querySelector('.images_first.active');

Your selector is incorrect. 您的选择器不正确。 .images_first active would select an element with tag name active that is a descendant of an element with class images_first . .images_first active将选择标签名称为 active的元素,该元素是images_first类的元素的后代 Ie it would find the inner element in this example: 即,它将在此示例中找到内部元素:

<div class="images_first">
  <active></active>
</div>

However, what you actually seem to want is to find the element that has both classes, images_first and active . 但是,您实际上想要的是找到同时具有images_firstactive两个类的元素。 This is written as 这写成

.images_first.active

Learn more about CSS selectors on MDN . 了解有关MDN上CSS选择器的更多信息


Related: Why does jQuery or a DOM method such as getElementById not find the element? 相关: 为什么jQuery或诸如getElementById之类的DOM方法找不到元素?

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

相关问题 为什么我无法访问 Javascript 中的样式元素? Firefox 控制台错误消息说它是“空” - Why can't I access the style element in Javascript? Firefox console error message says it's “Null” 添加事件监听器时出错,表示元素为null - Error adding event listener, says element is null JavaScript无法运行,表示element为null,但是代码运行得很混乱 - JavaScript won't run, says element is null, but code runs in fiddle 在控制台上打印 HTML 元素返回 NULL (javascript) - Printing HTML element on console returning NULL (javascript) 数组说它的长度是0,但是控制台说不 - Array Says Its Length Is 0 But Console Says Otherwise 过滤器没有返回数组的第一个元素,即使 console.log 说它是 - Filter is not returning the first element of the array, even though console.log says it is Javascript:console.log(arr.length)表示长度为n,console.log(arr)表示长度为n-1。 第二种情况不会显示最后一个元素 - Javascript: console.log(arr.length) says length is n, console.log(arr) says length is n-1. Second case won't show last element 控制台说找不到变量 - Console says cannot find a variable 错误控制台显示“未定义功能” - Error console says “function not defined” 浏览器在控制台中说“ reactComponent未定义” - Browser says 'reactComponent is not defined' in console
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM