[英]Add .addEventListener to a function
I have this following .js function: 我有以下.js函数:
function basketHandler(basket)
{
if (basket != null)
{
if(basket.Tickets.length > 0)
{
console.log("Basket is not null and has tickets");
}
else
{
console.log("Basket is not null but has no tickets");
}
}
else
{
console.log("Basket is null");
}
}
and I have been told I need to add an event listener to it, underneath the function, for it to work and output the relevant console.logs. 有人告诉我,我需要在该函数的下方添加一个事件侦听器,以使其起作用并输出相关的console.logs。 So after the functions I have tried the following with no joy...
所以在功能之后,我没有任何乐趣地尝试了以下操作...
basket.addEventListener("EVENTNAME", eventhandler)
basket.addEventListener("onload", basketHandler);
document.addEventListener("onload", basketHandler);
element.addEventListener("onload", basketHandler);
I am sure there is something fundamental I am missing, I can see the errors this code brings up in the console, but I don't know what they mean, so its difficult for me to fix it blindly... I don't have a great grasp of Javascript so I'm probably truing to run before I can walk, but either way, it's something I need to get working... any help greatly appreciated. 我确定我缺少一些基本的东西,我可以看到此代码在控制台中出现的错误,但是我不知道它们的含义,因此我很难盲目地修复它...我不知道掌握了Java脚本的精通知识,所以我可能会在走路之前就急于想跑步,但是无论哪种方式,这都是我需要开始工作的……任何帮助都将不胜感激。
EDIT - In response to: @David Thomas: The errors I'm getting in the console are just Uncaught ReferenceError: basket is not defined depending on which variation I try. 编辑-响应:@David Thomas:我在控制台中遇到的错误只是Uncaught ReferenceError:未定义篮子取决于我尝试的变化。
EDIT - I am also linked to a .js library here: https://tickets.leicesterymca.co.uk/Iframe/esrojsapi.js if that helps. 编辑-我也链接到.js库: https : //tickets.leicesterymca.co.uk/Iframe/esrojsapi.js(如果有帮助的话)。
I believe you mean addEventListener
. 我相信你的意思是
addEventListener
。 You're confusing it with the IE-only attachEvent
. 您将其与仅IE的
attachEvent
混淆了。
document.addEventListener("DOMContentLoaded", function() {
basketHandler(basket);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.