Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么活动没有';不执行?_Javascript_Events_Simple Html Dom - Fatal编程技术网

Javascript 为什么活动没有';不执行?

Javascript 为什么活动没有';不执行?,javascript,events,simple-html-dom,Javascript,Events,Simple Html Dom,嘿,伙计们,有人能解释一下为什么这个事件没有执行吗? 调试器根本没有给出任何错误! 先谢谢你 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <button class="boton">prueba</button> <body>

嘿,伙计们,有人能解释一下为什么这个事件没有执行吗? 调试器根本没有给出任何错误! 先谢谢你

    <!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>

  <button class="boton">prueba</button>
  <body>

<script type="text/javascript">

var elemento= document.getElementsByClassName("boton")
elemento.onclick=function(){
  alert("hola");
};
debugger;

</script>

  </body>
</html>

普鲁巴
var elemento=document.getElementsByClassName(“boton”)
elemento.onclick=function(){
警报(“hola”);
};
调试器;

DOM方法
document.getElementsByCassName()
返回一个数组。在分配
onclick
处理程序之前,必须循环遍历数组项:

var elemento=document.getElementsByClassName(“boton”)
对于(变量i=0;i

prueba
使用
var elemento=document.getElementsByClassName(“boton”)[0]
可能存在已工作的副本,非常感谢!