JavaScript基础
2012-11-13 17:55
232 查看
1.在Java中,如果要引入内部对象,应该用with(),例如:
with(Math);
document.write(cos(PI/3));
2.JavaScript对象
3.JavaScript日期对象
4.JavaScript简易计算器
with(Math);
document.write(cos(PI/3));
2.JavaScript对象
<!doctype html> <html> <head> <title>JavaScript</title> </head> <script> function Person(name, num, sex){ this.name = name; this.num = num; this.sex = sex; this.Show = Show; } function Show(){ document.write(this.name+" "+this.num+" "+this.sex); } var p = new Person("Jack",10011,"man"); p.Show(); </script> <body> </body> </html>
3.JavaScript日期对象
<!doctype html> <html> <head> <title>JavaScript</title> </head> <script> var now = new Date(); year = 1900 + now.getYear(); //年 month = 1 + now.getMonth(); //月 date = now.getDate(); //日 hour = now.getHours(); //时 minute = now.getMinutes(); //分 second = now.getSeconds(); //秒 document.write(year+"年"+month+"月"+date+"日"+hour+"时"+minute+"分"+second+"秒"); </script> <body> </body> </html>
4.JavaScript简易计算器
<!doctype html> <html> <head> <title>ACdreamer's calculator</title> </head> <script> function add() { var v1=parseInt(document.myform.text1.value); var v2=parseInt(document.myform.text2.value); document.myform.text3.value=v1+v2; } function subtract() { var v1=parseInt(document.myform.text1.value); var v2=parseInt(document.myform.text2.value); document.myform.text3.value=v1-v2; } function multiply() { var v1=parseInt(document.myform.text1.value); var v2=parseInt(document.myform.text2.value); document.myform.text3.value=v1*v2; } function divide() { var v1=parseInt(document.myform.text1.value); var v2=parseInt(document.myform.text2.value); document.myform.text3.value=v1/v2; } function ClearText() { document.myform.text1.value=""; document.myform.text2.value=""; document.myform.text3.value=""; } </script> <body> <center> <br><br><br><br><br> <h2><font color="green"><strong>A Magic Calculator</strong></font></h2> <h3><font color="blue">Made ACdreamer in the year of 2013.</font></h3> <form name="myform"> <font color="red"><strong>The First Input:</strong></font> <input type="text" name="text1"><br><br> <font color="red"><strong>The Second Input:</strong></font> <input type="text" name="text2"><br><br><br> <font color="red"><strong>Select Your Operation: </strong></font> <input type="button" value="+" onClick="add()"> <input type="button" value="-" onClick="subtract()"> <input type="button" value="*" onClick="multiply()"> <input type="button" value="/" onClick="divide()"> <input type="button" value="Clear" onClick="ClearText()"><br><br><br> <font color="red"><strong>Result Output:</strong></font> <input type="text" name="text3"><br> </form> </center> </body> </html>