首页 > 代码库 > Js闭包

Js闭包

<!DOCTYPE html><html><head>        <meta charset=UTF-8 />        <title></title><script>window.onload = function(){        var aLi = document.getElementsByTagName(li);        for (var i=0;i<aLi.length;i++){                (function(i){                        aLi[i].onclick = function(){                                alert(i);                        };                })(i);        }        };</script>        </head><body>    <ul>        <li>123</li>        <li>456</li>        <li>789</li>    </ul></body></html>

 

Js闭包