简体   繁体   English

HTML / Javascript addEventListener无法调用?

[英]HTML/Javascript addEventListener not calling?

<!DOCTYPE html>
<HTML>
<HEAD>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<style>
video {
  right: 0;
  bottom: 0;
  min-width: 50%;
  min-height: 50%;
  max-width: 90%;
  max-height:90%;
  width: auto;
  height: auto;
</style>
</HEAD>
<BODY bgcolor=black>
<center><video width="853" height="480" autoplay="autoplay">
  <source src="file:///C:/Program Files/NARUTO SHIPPUDEN Ultimate Ninja STORM 2/IntroMovie.mp4" type="video/mp4" />
  Your browser does not support HTML5 video.
</video></center>
</BODY>
<script type="text/javascript">
document.body.addEventListener("onclick", function(){
    alert("Hello! I am an alert box!!");
});
document.body.addEventListener("oncontextmenu", function(){
    alert("Hello! I am an alert box!!");
});
document.body.addEventListener("onkeypress", function(){
    alert("Hello! I am an alert box!!");
});
</script>
</HTML>

For some reason whenever I click on the body (Whether inside or outside of the video) the alert boxes don't pop up. 由于某种原因,无论何时我单击主体(无论在视频内部还是外部),警报框都不会弹出。

Is there any reason why the code isn't showing the alert boxes? 有什么原因导致代码不显示警报框?

It should be: 它应该是:

document.body.addEventListener("click", function(){
    alert("Hello! I am an alert box!!");
});

You're adding an unnecessary 'on' in front of each action (which is necessary with attachEvent, but not addEventListener). 您在每个动作前都添加了一个不必要的“ on”(attachEvent是必需的,而addEventListener则不需要)。

Should be: 应该:

  • click 点击
  • keypress 按键
  • contextmenu 上下文菜单
  • etc 等等

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

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