title: JavaScript get the right zero-based index of clicked link
body: <p>An alert should display anchor's zero-based index within a document instead of following the link.
  HTML: </p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
    &lt;head&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;title&gt;Test page&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
      In my life, I used the following web search engines:&lt;br/&gt;
      &lt;a href="//www.yahoo.com"&gt;Yahoo!&lt;/a&gt;&lt;br/&gt;
      &lt;a href="//www.altavista.com"&gt;AltaVista&lt;/a&gt;&lt;br/&gt;
      &lt;a href="//www.google.com"&gt;Google&lt;/a&gt;&lt;br/&gt;
      &lt;script type="text/javascript" src="js/js8.js"&gt;&lt;/script&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>JavaScript:</p>
  
  <pre><code>function registerHandlers() {
    var as = document.getElementsByTagName('a');
    for (i = as.length; i-- &gt;= 0;) {
      as[i].onclick = function() {
        alert(i);
        return false;
      }
    }
  }
  </code></pre>
  
  <p>I tried to use jQuery:</p>
  
  <pre><code>$('a').click(function(event) {
      var as = document.getElementsByTagName('a');
      alert($(this).index());
  });
  </code></pre>
  
  <p>but I got the index like 1,3,5 not 0,1,2</p>