对象Javascript中的标签无效 - 想要添加事件监听器

时间:2012-08-31 00:39:03

标签: javascript label addeventlistener

({
photo:  foo.addEventListener("animationend", photoCycle, true) ||  foo.addEventListener("webkitAnimationEnd", photoCycle, true),

bio: foo.addEventListener("animationend", bioCycle, true) ||  foo.addEventListener("webkitAnimationEnd", bioCycle, true)
})[mode]

我正在尝试创建一个对象来评估模式(以太网为photobio)并根据它是firefox animationend还是chrome {添加一个事件监听器{1}}。奇怪的是,当模式为webkitanimationend时,photo键的无效标签会出现语法错误。

1 个答案:

答案 0 :(得分:1)

此构造为未命名的对象属性赋值。此外,选择正确的事件名称的方法也不起作用,因为addEventListener是两个浏览器上的有效函数。

以下是一些代码:

var fn = (mode == "photo") ? photoCycle : bioCycle;
var eventName = ((navigator.userAgent.indexOf("WebKit") != -1) ? "webkitAnimationEnd" : "animationEnd";

foo.addEventListener(eventName, bioCycle, true)