JavaScript基础
switch
<script language="javascript">
var seson = prompt("请输入数字(1~4)");
switch (seson) {
case "1": msg = "你选择的春天"; break;
case "2": msg = "你选择的夏天"; break;
case "3": msg = "你选择的秋天"; break;
case "4": msg = "你选择的冬天"; break;
default: msg = "你选择的不对"; break;
}
document.write(msg);
</script>
多个函数同时调用
<script language="javascript">
<!--
function fun1(){
alert("这是fun1");
}
function fun2(){
alert("这是fun2");
}
//-->
</script>
</head>
<body>
<input type="button" value="单击我" onClick="fun1(),fun2()">
</body>
绝对值函数的应用
<script language="javascript">
<!--
document.write("0的绝对值为:",Math.abs(0),"<br>");
document.write("1的绝对值为:",Math.abs(1),"<br>");
document.write("-1的绝对值为:",Math.abs(-1),"<br>");
//-->
</script>
try catch处理
<script language="javascript">
<!--
try{
a[1] = 10;
}catch(e){
alert("错误:"+e);
}
-->
</script>
连接两个数组
<script language="javascript">
<!--
a = new Array("a","b","c");
b = new Array(1,2,3);
c = a.concat(b);
for(i=0;i<c.length;i++){
document.write(c[i],"<br>");
}
//-->
</script>
平方根函数的使用
<script language="javascript">
<!--
n = prompt("请输入一个要平方根的数:");
document.write(n,"的平方根为:",Math.sqrt(n));
//-->
</script>
得到一个数的平方
<script language="javascript">
<!--
radius = prompt("请输入一个数:");
area =Math.pow(radius, 2);
document.write("这个数的平方是:" + area);
//-->
</script>
取得对象的信息
<body>
<script language="javascript">
<!--
function showID(obj){
alert(obj.id);
}
//-->
</script>
<input type="button" value ="显示" id="bt" onClick="showID(this)">
</body>
取整函数的使用
<body>
<script language="javascript">
<!--
n = prompt("请输入一个要取整的数:");
if(n>0){
int = Math.floor(n);
}else{
int = Math.ceil(n);
}
document.write(n,"的整数为:",int);
//-->
</script>
</body>
取得类别名称
<style type="text/css">
<!--
.bgcolor{
background-color:#993300;
}
-->
</style>
</head>
<body>
<input type="button" value ="显示" id="bt" onClick="alert
(bt.className)" class="bgcolor">
</body>