js的onload實現:
. 代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js的onload方法的使用</title>
<script type="text/javascript">
window.onload=function(){
var myLinks = document.getElementsByTagName("a");
for(var i=0 ; i<myLinks.length; i++){
myLinks[i].onclick=function(){
alert("鏈接了");
}
}
}
</script>
</head>
<body>
<h1>js的onload方法的使用</h1>
<a href="#">鏈接1</a><br/>
<a href="#">鏈接2</a><br/>
<a href="#">鏈接3</a><br/>
<a href="#">鏈接4</a><br/>
<a href="#">鏈接5</a><br/>
<a href="#">鏈接6</a><br/>
</body>
</html>
jquery的ready方法的實現:
. 代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ready方法的使用2</title>
<script type="text/javascript" src="jquery-core/jquery-1.8.0.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("a").click(function(){
alert("鏈接了!");
});
});
</script>
</head>
<body>
<h1>ready方法的使用2</h1>
<a href="#">鏈接1</a><br/>
<a href="#">鏈接2</a><br/>
<a href="#">鏈接3</a><br/>
<a href="#">鏈接4</a><br/>
<a href="#">鏈接5</a><br/>
<a href="#">鏈接6</a><br/>
</body>
</html>