首页javascriptmouseJavascript Event - 如何向锚点元素添加单击事件处理程序

Javascript Event - 如何向锚点元素添加单击事件处理程序

我们想知道如何向锚点元素添加单击事件处理程序。

<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
window.onload=function(){
    var myArray = [
      {property: "value1"},
      {property: "value2"},
      {property: "value3"}
    ];
    var pageLinks = document.getElementsByTagName("a");
    for (i = 0, len = pageLinks.length; i < len; i++){
        (function(i){
            pageLinks[i].addEventListener("click", function(){
                document.writeln(myArray[i].property);
                document.writeln('<br/>');
            }, false);
        })(i);
    }
}
</script>
</head>
<body>
  <a href="#">link1</a>
  <a href="#">link2</a>
  <a href="#">link3</a>
</body>
</html>