<%@ page language="java" contentType="text/html; charset=gbk" pageEncoding="gbk"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gbk"> <title>Insert title here</title> <mce:script type="text/javascript"><!-- function test(){ var btBody = document.getElementById('test44').childNodes[1]; var ids=[1,2,3,6,9,20,21,34,45,67,68]; for(var i = 0 ; i< ids.length ; i++){ var tr = document.createElement('tr'); var td = document.createElement('td'); var td1 = td.cloneNode(false); td1.appendChild(document.createTextNode(ids[i])); var td2 = td.cloneNode(false); td2.appendChild(document.createTextNode(ids[i]+"ssss")); var td3 = td.cloneNode(false); td3.appendChild(document.createTextNode(ids[i]+"aaaaa")); tr.appendChild(td1); tr.appendChild(td2); tr.appendChild(td3); tr.attachEvent("onclick",function(i){ return function(){alert(i)}}(i)); // tr.οnclick=function(i){return function(){alert(i)}}(i) btBody.appendChild(tr); } } // --></mce:script> </head> <body> <input type="button" value="test" οnclick="test();"></input> <table id="test44" cellspacing="1" border="1" class="tablesorter"> <thead> <tr> <th> First Name </th> <th> Second Name </th> <th> Age </th> </tr> </thead> <tbody> </tbody> </table> </body> </html>
JavaScript闭包例子
最新推荐文章于 2024-03-09 09:00:00 发布