弹出当前日期
<script type="text/javascript">
alert(new Date().toLocaleDateString())
</script>
调用js文件
<script src="js路径" type="text/javascript"></script>
js里面用var声明变量
计算一百内所有数字的和js里面
<script type="text/javascript">
var sum = 0;
for (var i = 1; i <= 100; i++) {
sum = sum + i
}
alert(sum);
</script>
匿名函数
var 名称 = function()
js里面的数组
var names = new Array();
names[0] = "小红";
names[1] = "小亮";
names[2] = "小强";
names[3] = "小明";
for (var i = 0; i < names.length; i++) {
alert(names[i]);
}
数组里求最大值
function getMax(arr) {
var max = arr[0];
for (var i = 0; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
var arr1 = new Array();
arr1[0] = 11;
arr1[1] = 22;
arr1[2] = 33;
alert(getMax(arr1));
forearch遍历
var dict = new Array();
dict["人"] = "ren";
dict["口"] = "kou";
dict["数"] = "shu";
for(var k in dict){
alert(k);
}
<script type="text/javascript">
alert(new Date().toLocaleDateString())
</script>
调用js文件
<script src="js路径" type="text/javascript"></script>
js里面用var声明变量
计算一百内所有数字的和js里面
<script type="text/javascript">
var sum = 0;
for (var i = 1; i <= 100; i++) {
sum = sum + i
}
alert(sum);
</script>
匿名函数
var 名称 = function()
js里面的数组
var names = new Array();
names[0] = "小红";
names[1] = "小亮";
names[2] = "小强";
names[3] = "小明";
for (var i = 0; i < names.length; i++) {
alert(names[i]);
}
数组里求最大值
function getMax(arr) {
var max = arr[0];
for (var i = 0; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
var arr1 = new Array();
arr1[0] = 11;
arr1[1] = 22;
arr1[2] = 33;
alert(getMax(arr1));
forearch遍历
var dict = new Array();
dict["人"] = "ren";
dict["口"] = "kou";
dict["数"] = "shu";
for(var k in dict){
alert(k);
}