[英]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++;
});
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_first
和active
两个类的元素。 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.