为什么我不能在我的javascript代码中创建“onclick”事件处理程序?

时间:2015-03-22 15:24:09

标签: javascript html events dom onclick

当我把" onclick"处理程序作为属性。像

<div class="myDiv" onclick="window.location='http://www.google.com/'"></div>

但是当我这样做时它不起作用

<!DOCTYPE html>
<html>
  <head>
    ... Around here has "title","meta" and "css link", then ...
    <script src="myEvent.js"></script>
  </head>
  <body>
    ... Any thing here ...
    <div class="myDiv"></div>
    ... Any thing here ...
  </body>
</html>

和&#34; myEvent.js&#34;看起来像

var myDiv = document.getElementsByClassName("myDiv");
myDiv.onclick = function(){
  window.location = "http://www.google.com/
};

谢谢!

0 个答案:

没有答案