VGIS☆紫微星

学习C#,展望GIS,努力成就VGIS

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">
 8window.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}

20function popUp(winURL) {
21  window.open(winURL,"popup","width=320,height=480");
22}

23
</script>
24</head>
25<body>
26<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>    
 8window.load=alink();
 9function 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}

18function popUp(winURL)
19{
20    window.open(winURL,"popup","width=320,height=480");    
21}

22
</script>
23</head>
24<body>
25<href="http://www.baidu.com" class=popUp>baidu</a>
26</body>
27</html>
28

©2008 VGIS☆紫微星 转帖请注明出处,留此信息。

本帖子以现状提供且没有任何担保,同时也没有授予任何权利。
This posting is provided "AS IS" with no warranties, and confers no rights.

Tag标签: JavaScript,function

posted on 2008-06-12 14:17 紫微星 阅读(220) 评论(3)  编辑 收藏 所属分类: JavaScript

评论

#1楼  2008-06-13 14:02 Solog      

这个有点像匿名函数。可以不写函数名的。
不能说哪个不规范,如果函数不重用。就不用提出那个FUNC成一个独立FUNC了   回复  引用  查看    

#2楼 [楼主] 2008-06-13 15:53 紫微星      

@Solog
恩,我后来查了《JavaScript权威指南》,知道函数的定义有三种方法。
谢谢你的回复!
  回复  引用  查看    

#3楼  2008-06-25 17:00 love&tiger      

上边写的那个,比你自己练习的就规范
不写函数名很常见   回复  引用  查看    


标题  
姓名  
主页
Email (只有博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      


相关链接: