<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript"> window.onload = function(){//页面所有元素加载完毕 var items = document.getElementsByTagName("p");//获取页面中的所有p元素 for(var i=0;i < items.length;i++){ //循环 items[i].onclick = function(){ //给每一个p添加onclick事件 //doing something... alert("suc!"); } } } </script> </head> <body> <p>测试1</p> <p>测试2</p> </body> </html>