【发布时间】:2015-04-07 20:53:40
【问题描述】:
我正在努力弄清楚为什么我的代码不起作用。这是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 );
});
}
这是 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>
由于某种原因,当我加载页面时,我收到此错误:
TypeError: undefined is not a function (evaluating 'showMenu.addEventListener')
我不明白这一点,因为如果我改变这一行:
var showMenu = document.getElementsByClassName('showMenu'),
到:
var showMenu = document.getElementById( 'showMenu' ),
确实有效!
为什么类选择器不起作用,而 id 选择器起作用?我正在尝试获取与 showMenu 类的两个链接来执行 JS。
【问题讨论】:
标签: javascript jquery html web