1. Add onclick events to all p elements in the web page:
Copy the code code as follows:
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<title>Insert title here</title>
<!-- <script src="jQuery/jquery-1.10.2.js"></script>-->
<script type="text/javascript">
window.onload=function(){
var items=document.getElementsByTagName("p");
for(i=0;i<items.length;i++){
items[i].onclick=function(){
alert("Click successful...");
}
}
}
</script>
</head>
<body>
<p>Test paragraph one...</p>
<p>Test paragraph two...</p>
<p>Test paragraph three...</p>
</body>
</html>
2. Make every other row of a specific table change color:
Copy the code code as follows:
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<title>Insert title here</title>
<!-- <script src="jQuery/jquery-1.10.2.js"></script>-->
<script type="text/javascript">
window.onload=function(){
var item=document.getElementById("tb");
var tbody=item.getElementsByTagName("tbody")[0];
var trs=tbody.getElementsByTagName("tr");
for(var i=0;i<trs.length;i++){
if(i%2==0){
trs[i].style.backgroundColor="green";
}
}
}
</script>
</head>
<body>
<table id="tb">
<tbody>
<tr><td>First line</td></tr>
<tr><td>Second line</td></tr>
<tr><td>The third line</td></tr>
<tr><td>The fourth line</td></tr>
<tr><td>The fifth line</td></tr>
<tr><td>Sixth line</td></tr>
</tbody>
</table>
</body>
</html>