<html>
<head>
<title> JavaScript</title>
<meta charset="UTF-8">
<script type="text/javascript">
/* document.write("<h1> hello world!</h1>");//相当于System.out.println()
document.write("<h1> hello world!</h1>");//注意:基本不再使用
document.write("<h1> hello world!</h1>");*/
console.log("这是我们的后台输出"); //开启Chrome浏览器后台调试功能
/**********************变量定义************************/
// var num = 10; //定义变量只需要一个var即可,类型根据赋值结果决定
/* 小数和整数都为 number 类型
* 字符串 和 字符 都为String 类型
*/
// console.log(typeof num); //控制台输出num 的类型为number
// var num1;
/*1. if(num1 == undefined){ //num1 未赋值类型为undefined
alert("变量num1类型为undefined"); //输出
}*/
/*2. if(!num1){
alert("变量num1没有内容");//√ true
}else{
alert("变量num1有内容"); //false
}*/
/*************拆分字符串************/
// var num = 'Hello world come on'; //198.168.1.1也可以拆分!!!直接替换就可以
// var result = num.split("");
// document.write(typeof result); //result类型为Object
// for(var x= 0; x < result.length; x ++){
// document.write(result[x]); //拆分后的内容
// }
/*************字符串比较************/
// var stra = 'hello';
// var strb = 'hello';
// alert(stra == strb); //true,不需要使用equals;
/*
*******************************************************
使用JavaScript输出表格(乘法口诀)
*******************************************************
*/
/* document.write( "<table border='1'>" );
for(var x = 1; x < 10; x ++){
document.write("<tr>")
for(var y = 1; y <= x; y ++){
document.write("<td>"+ x + "*" + y + "=" + (x*y) + "</td>");
}
for(var y = 1; y <= 9-x ;y ++){
document.write("<td> </td>"); //填充没有内容的表格
}
}
document.write("</table>")
------------------Result------------------
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
------------------------------------------
*/
/*
********************************************************
数组
********************************************************
var result = new Array();
result[0] = 'hello';
result[1] = 100;
result[2] = true;
//或者静态初始化var result = new Array('hello',100,true);
for(var x= 0; x < result.length; x ++){
document.write("<h1>"+result[x]+"-->"+typeof result[x]+"</h1>");
}
-----------------result---------------------
hello-->string
100-->number
true-->boolean
好处在于没有长度限制,天生就为动态数组。
*/
</script>
</head>
<body>
<div>世界,你好!</div>
</body>
</html>
JavaScript-简单语法1
最新推荐文章于 2023-12-14 15:01:08 发布