简体   繁体   English

Javascript - 无法让'getElementsByClassName'正常工作

[英]Javascript - Can't get 'getElementsByClassName' working

Im struggling to figure out why my code is not working. 我正在努力弄清楚为什么我的代码不起作用。 Here is part of the JS: 这是JS的一部分:

function init() {
    var showMenu = document.getElementsByClassName('showMenu'),
        perspectiveWrapper = document.getElementById( 'perspective' ),
        container = perspectiveWrapper.querySelector( '.container' ),
        contentWrapper = container.querySelector( '.wrapper' );

    showMenu.addEventListener( clickevent, function( ev ) {
        ev.stopPropagation();
        ev.preventDefault();
        docscroll = scrollY();
        // change top of contentWrapper
        contentWrapper.style.top = docscroll * -1 + 'px';
        // mac chrome issue:
        document.body.scrollTop = document.documentElement.scrollTop = 0;
        // add modalview class
        classie.add( perspectiveWrapper, 'modalview' );
        // animate..
        setTimeout( function() { classie.add( perspectiveWrapper, 'animate' ); }, 25 );
    });
}

Here is part of the HTML: 这是HTML的一部分:

<div id="topBar">
    <h1>Company</h1>
    <a href="#" class="entypo-menu showMenu"></a>
</div>

<div class="line"></div>

<div id="fixedBar">
    <h1>Company</h1>
    <a href="#" class="entypo-menu showMenu"></a>
</div>

For some reason when I load the page, I get this error: 出于某种原因,当我加载页面时,我收到此错误:

TypeError: undefined is not a function (evaluating 'showMenu.addEventListener')

I don't understand this because if I change this line: 我不明白这一点,因为如果我改变这一行:

var showMenu = document.getElementsByClassName('showMenu'),

to: 至:

var showMenu = document.getElementById( 'showMenu' ),

It does work! 它确实有效!

Why won't the class selector work but the id one will? 为什么类选择器不起作用,但id会不会? I'm trying to get both links with the class showMenu to perform the JS. 我正在尝试使用类showMenu获取两个链接以执行JS。

document.getElementsByClassName returns an array-like list (an HTMLCollection , to be exact) of all elements that match the class name. document.getElementsByClassName返回与类名匹配的所有元素的类似数组的列表( HTMLCollectionHTMLCollection )。 You probably care about the first element, so try using the following instead: 您可能关心第一个元素,因此请尝试使用以下代码:

var showMenu = document.getElementsByClassName( 'showMenu' )[0],

If you care about all of the elements, you'll need to loop through the elements: 如果您关心所有元素,则需要遍历元素:

var showMenu = document.getElementsByClassName( 'showMenu' ),

// ...

for ( var i = 0; i < showMenu.length; ++i ) {
    showMenu[i].addEventListener( clickevt, function( ev ) {
        // Your code here
    });
}

That's because document.getElementsByClassName() returns a node list , which is similar to an array. 那是因为document.getElementsByClassName()返回一个节点列表 ,类似于一个数组。

So if you have multiple elements you wish to target, you'll have to loop through them with a for loop or .forEach() but if you're only looking for the nth element with this class then you can specify it's index with bracket notation: 因此,如果你想要定位多个元素,你必须使用for循环或.forEach()循环它们,但是如果你只是用这个类来寻找第n个元素,那么你可以用括号指定它的索引符号:

var showMenu = document.getElementsByClassName('showMenu')[0],

It's because getElementsByClassName return an array-like object of DOM elements . 这是因为getElementsByClassName返回DOM元素的类数组对象

You need to access each element and attach the event listener individually: 您需要访问每个元素并单独附加事件侦听器:

for (var i = 0; i < showmenu.length; i++ ) {
    showMenu[i].addEventListener( clickevent, function( ev ) {
        // ...
    });
}

..or: ..要么:

Array.prototype.forEach.call(showMenu, function(el, i) {
    el.addEventListener( clickevent, function( ev ) {
        // ...
    });
});

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

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