<!DOCTYPE html>
<html> <head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript">
window.onload = function() {
var items = document.getElementsByTagName("p");
for(var i = 0; i < items.length; i++) {
items[i].onclick = function() {
alert("suc!");
}
} };
</script>
</head> <body>
<p>测试1</p>
<p>测试2</p> </body> </html>
JavaScript例子1-给网页中所有
元素添加onclick事件