2017-09-25 67 views
0

我已经开始创造某种形象滑块向右,但在beggining我已经达到了一个问题控制台说元素是“空”

控制台说,我IMG1和其他人都是“零” ..

我的错误在哪里?

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

+0

他们可能还不存在。 – SLaks

+3

它应该是'.images_first.active' – Turnip

回答

1

你的选择是不正确。 .images_first active将选择一个元素标签名称active这是一个后裔元素与类images_first。即它会在这个例子中发现的内部元件:

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

然而,实际上似乎想要的是找到一个具有两个类,images_firstactive的元素。这写作

.images_first.active 

Learn more about CSS selectors on MDN


相关:Why does jQuery or a DOM method such as getElementById not find the element?

+0

谢谢,但现在它说img2没有定义。我的意思是当你点击向前它不能添加一个类到这个元素,因为它是“未定义的” – Matt

+0

@Matt:不,错误表示它不能在'undefined'上访问属性'add'。这是因为你在'img2.classlist'中有一个错字。 'list'的'l'应该大写:'img2.classList',就像前一行一样。 'img2'本身被定义。 –

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