<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>获取li的index</title> </head> <body> <ul> <li>《登鹳雀楼》</li> <li>白日依山尽,</li> <li>行河入海流。</li> <li>欲穷千里目,</li> <li>更上一层路。</li> </ul> <script> var li = document.getElementsByTagName(‘li‘); for(var i=0;i<li.length;i++) { li[i].index = i; li[i].onclick = function(){ alert(this.index); } } </script> </body> </html>
时间: 2024-11-02 15:39:53