学习javascript
下面的代码实现了javascript文件的引用:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="JavaScript1.js" type="text/javascript"></script>
<script type="text/javascript" >
alert(new Date().toLocaleDateString());
</script>
</head>
<body>
</body>
</html>
另外,在超链接的点击里可以执行javascript:
<a href="javascript:alert('hahaha')">js事件</a>,当点击浏览器的 js事件 超链接时,弹框 hahaha
下面的代码实现了f1函数的调用,alert语句必须被包在script标签内。
<script type="text/javascript">
f1 = function add(i1, i2) {
return i1 + i2;
}
alert(f1(1,2));
</script>
javascript也有面向对象的用法:
<script type="text/javascript">
function person(name, age) {
this.Name = name;
this.Age = age;
this.say=function(){alert(this.Name+this.Age+"hello");}
}
new person("tom", "12").say();
</script>
可以像下面new个数组指向maximum函数,调用函数内的语句:
<script type="text/javascript">
function maximum(arr) {
var max = arr[0];
for (var i = 0; i < arr.length; i++) {
if (arr[i + 1] > max) {
max = arr[i + 1];
}
}
return max;
}
var arr1 = new Array();
arr1[0] = 1;
arr1[1] = 2;
arr1[2] = 3;
alert(maximum(arr1));
</script>