JavaScript中有关函数的疑问
1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
2
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
3
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
4
<head>
5
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
6
<title>Example</title>
7
<script type="text/JavaScript">
8
window.onload = function() {
9
if (!document.getElementsByTagName) return false;
10
var lnks = document.getElementsByTagName("a");
11
for (var i=0; i<lnks.length; i++) {
12
if (lnks[i].getAttribute("class") == "popup") {
13
lnks[i].onclick = function() {
14
popUp(this.getAttribute("href"));
15
return false;
16
}
17
}
18
}
19
}
20
function popUp(winURL) {
21
window.open(winURL,"popup","width=320,height=480");
22
}
23
</script>
24
</head>
25
<body>
26
<a href="http://www.example.com/" class="popup">Example</a>
27
</body>
28
</html>
上面是书中的示例。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"2
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">3
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">4
<head>5
<meta http-equiv="content-type" content="text/html; charset=utf-8" />6
<title>Example</title>7
<script type="text/JavaScript">8
window.onload = function() {9
if (!document.getElementsByTagName) return false;10
var lnks = document.getElementsByTagName("a");11
for (var i=0; i<lnks.length; i++) {12
if (lnks[i].getAttribute("class") == "popup") {13
lnks[i].onclick = function() {14
popUp(this.getAttribute("href"));15
return false;16
}17
}18
}19
}20
function popUp(winURL) {21
window.open(winURL,"popup","width=320,height=480");22
}23
</script>24
</head>25
<body>26
<a href="http://www.example.com/" class="popup">Example</a>27
</body>28
</html>下面这个是我自己练习写的
疑问主要是:
1.function可以没有函数名?如书中示例所写?
2.onclick后面最规范的写法是哪种?
1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2
<html xmlns="http://www.w3.org/1999/xhtml">
3
<head>
4
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5
<title>Image Gallery</title>
6
<link rel="stylesheet" type="text/css" href="css/layout.css" />
7
<script type=text/Javascript>
8
window.load=alink();
9
function alink()
10
{
11
var alink=document.getElementsByTagName("a")
12
for(i=0;i<links.length;i++)
13
{
14
if(alinks[i].className="popUp"
15
alink[i].onclick=popUp(this.getAttribute("href");
16
}
17
}
18
function popUp(winURL)
19
{
20
window.open(winURL,"popup","width=320,height=480");
21
}
22
</script>
23
</head>
24
<body>
25
<a href="http://www.baidu.com" class=popUp>baidu</a>
26
</body>
27
</html>
28
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">2
<html xmlns="http://www.w3.org/1999/xhtml">3
<head>4
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />5
<title>Image Gallery</title>6
<link rel="stylesheet" type="text/css" href="css/layout.css" />7
<script type=text/Javascript> 8
window.load=alink();9
function alink()10
{11
var alink=document.getElementsByTagName("a")12
for(i=0;i<links.length;i++)13
{14
if(alinks[i].className="popUp"15
alink[i].onclick=popUp(this.getAttribute("href"); 16
}17
}18
function popUp(winURL)19
{20
window.open(winURL,"popup","width=320,height=480"); 21
}22
</script>23
</head>24
<body>25
<a href="http://www.baidu.com" class=popUp>baidu</a>26
</body>27
</html>28




window.onload
浙公网安备 33010602011771号