首页 > 代码库 > JavaScript基础
JavaScript基础
/*
Function:nmn
Description:打印九九乘法表
Author:Nick.Chung
Create:2017-06-19
Parameter:None
Return:None
*/
var nmn = function () {
for (var i = 1; i <= 9; i++) {
for (var j = 1; j <= i; j++) {
document.write(
i.toString() + "x" + j.toString() //输出ixj字符
+ "=" + i * j //输出i*j乘积
+ " "); //输出空格
}
document.write("<br>"); //输出换行符(每行)
}
}
//以表格形式打印九九乘法表
var nmnWithTable = function () {
document.write("<table>")
for (var i = 1; i <= 9; i++) {
document.write("<tr>");
for (var j = 1; j <= i; j++) {
document.write(
"<td style=‘border:1px solid gray;‘>"
+ i.toString() + "x" + j.toString() //输出ixj字符
+ "=" + i * j //输出i*j乘积
+ " ") //输出空格
+ "</td>";
}
document.write("</tr>");
}
document.write("</table>");
}
//求算术
var square = function (type) {
var input = prompt("请输入数字");
//如果是数字
if (input.length > 0 && !isNaN(input)) {
var num = parseInt(input);
switch (type) {
case "2T":
alert(Math.pow(num, 2));
break;
case "3T":
alert(Math.pow(num, 3));
break;
case "SQRT":
alert(Math.sqrt(num, 2));
break;
case "ROUND":
alert(Math.round(num));
break;
default:
break;
}
}
else {
alert("不是数字!请重新输入");
}
}
//画等腰三角型
var writeHR = function () {
for (var i = 1; i <= 30; i++) {
document.write("<hr style=‘width:" + i * 10 + "px‘ />");
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script type="text/javascript" src="http://www.mamicode.com/Scripts/common.js"></script> </head> <body> <script type="text/javascript"> document.writeln(typeof (1) + ‘<br>‘); document.writeln(typeof (NaN) + ‘<br>‘); document.writeln(typeof (Number.MIN_VALUE) + ‘<br>‘); document.writeln(typeof (Infinity) + ‘<br>‘); document.writeln(typeof ("123") + ‘<br>‘); document.writeln(typeof (true) + ‘<br>‘); document.writeln(typeof (window) + ‘<br>‘); document.writeln(typeof (document) + ‘<br>‘); document.writeln(typeof (null) + ‘<br>‘); document.writeln(typeof (eval) + ‘<br>‘); document.writeln(typeof (Date) + ‘<br>‘); document.writeln(typeof (sss) + ‘<br>‘); document.writeln(typeof (undefined) + ‘<br>‘); var arr = ["A", "B", "C", "D", "E"]; var msg = ""; for (var i = 0; i < arr.length; i++) { msg += arr[i] + ‘<br>‘; } var joinMsg = arr.join(‘,‘); document.write(msg); document.write(joinMsg + "<br>"); var article = ["我在南方学院读书!", "我的兴趣是什么?", "我的梦想是什么?"] var result = article.join(‘<br>‘) document.write(result + "<br>"); function doSomething() { var newMsg = ""; for (i = 0; i < 10; i++) { if (i == 3) continue; newMsg = newMsg + "The number is " + i + "<br>"; } return newMsg; } function howManyArgs() { document.write(arguments.length + "<br>"); } howManyArgs("string", 45); howManyArgs(); howManyArgs(12); function f1() { var n = 999; function f2() { document.write(n + "<br>"); n++; } return f2; } var result = f1(); result(); // 999 result(); // 1000 nmn(); nmnWithTable(); square(‘ROUND‘); writeHR(); </script> <script type="text/javascript"> var timeAlert = function () { var dt = new Date(); var dtStr = dt.toLocaleString(); var hour = dt.getHours(); var msg = ""; if (hour >= 6 && hour < 9) { msg = "早上好"; } else if (hour >= 9 && hour <= 12) { msg = "上午好"; } else if (hour > 12 && hour <= 18) { msg = "下午好"; } else { msg = "晚上好"; } alert(dtStr + "," + msg); } timeAlert(); </script> <table border=1> <tr name=‘th‘><td>折扣价</td><td>出发日期</td><td>出发地</td><td>目标地</td><td>折扣</td></tr> <tr name=‘tr‘><td>$267</td><td>2015-06-05</td><td>北京</td><td>南京</td><td>1.6折</td></tr> <tr name=‘tr‘><td>$267</td><td>2015-06-05</td><td>北京</td><td>南京</td><td>1.6折</td></tr> <tr name=‘tr‘><td>$267</td><td>2015-06-05</td><td>北京</td><td>南京</td><td>1.6折</td></tr> <tr name=‘tr‘><td>$267</td><td>2015-06-05</td><td>北京</td><td>南京</td><td>1.6折</td></tr> <tr name=‘tr‘><td>$267</td><td>2015-06-05</td><td>北京</td><td>南京</td><td>1.6折</td></tr> <tr name=‘tr‘><td>$267</td><td>2015-06-05</td><td>北京</td><td>南京</td><td>1.6折</td></tr> </table> <script type="text/javascript"> var trArr = document.getElementsByName(‘tr‘); for (var i = 0; i < trArr.length; i++) { if (i % 2 == 0) { trArr[i].style.backgroundColor = ‘red‘; } else { trArr[i].style.backgroundColor = ‘blue‘; } } </script> </body> </html>
JavaScript基础