JavaScript按类名获取元素

时间:2014-12-18 01:20:26

标签: javascript html css tweenlite tweenmax

为什么我的getElementsByClassName代码不起作用?它在使用getElementById时有效但在类中没有用吗?我正在使用TweenLite扩展。

我的代码不起作用 - http://codepen.io/bleubateau/pen/pvbeaO 与ID一起使用的相同代码 - http://codepen.io/bleubateau/pen/ogLZqb 与类http://codepen.io/bleubateau/pen/PwzpQj

一起使用的类似代码
window.onload = function() {
  var play = document.getElementById("lay");

play.onmouseover = function(){
    TweenMax.to(play, 0.5, {width:"120px", marginLeft:"-60px", marginTop:"-60px", repeat:-1, repeatDelay:0.1, yoyo:true});

};

play.onmouseout = function(){
    TweenLite.to(playBTN, 1, {width:"100px", marginLeft:"-50px", marginTop:"-50px"});
};
}

1 个答案:

答案 0 :(得分:3)

getElementsByClassName()会生成一个节点列表,因此即使您的列表只包含一个项目,您仍然需要像这样访问它:

var play = document.getElementsByClassName('lay')[0];