《Javascript基础知识总结(代码篇).doc》由会员分享,可在线阅读,更多相关《Javascript基础知识总结(代码篇).doc(8页珍藏版)》请在金锄头文库上搜索。
1、Javascript总结(代码篇)1)Alert boxfunction disp_alert()alert(I am an alert box!)(2) confirm boxfunction disp_confirm() var r=confirm(Press a button) if (r=true) document.write(You pressed OK!) else document.write(You pressed Cancel!) (3)Prompt boxfunction disp_prompt() var name=prompt(Please enter your na
2、me,Harry Potter) if (name!=null & name!=) document.write(Hello + name + ! How are you today?) (4) Call a function1function myfunction()alert(HELLO)By pressing the button, a function will be called. The function will alert a message.(5)Function with an argumentfunction myfunction(txt)alert(txt)By pre
3、ssing the button, a function with an argument will be called. The function will alertthis argument.(7)Function with an argument 2function myfunction(txt)alert(txt)When you click on one of the buttons, a function will be called. The function will alertthe argument that is passed to it.(8)Function_ret
4、urnfunction myFunction()return (Hello, have a nice day!)document.write(myFunction()The script in the body section calls a function.The function returns a text.(9)function product(a,b)return a*bdocument.write(product(4,3)The script in the body section calls a function with two parameters (4 and 3).Th
5、e function will return the product of these two parameters.(10)forfor (i = 0; i = 5; i+)document.write(The number is + i)document.write()Explanation:This for loop starts with i=0.As long as i is less than, or equal to 5, the loop will continue to run.i will increase by 1 each time the loop runs.(11)
6、for (i = 1; i = 6; i+)document.write(This is header + i)document.write()(12)whilei = 0while (i = 5)document.write(The number is + i)document.write()i+Explanation:i is equal to 0.While i is less than , or equal to, 5, the loop will continue to run.i will increase by 1 each time the loop runs.(13)do.w
7、hilei = 0dodocument.write(The number is + i)document.write()i+while (i = 5)Explanation:i equal to 0.The loop will runi will increase by 1 each time the loop runs.While i is less than , or equal to, 5, the loop will continue to run.(14)breakvar i=0for (i=0;i=10;i+)if (i=3)breakdocument.write(The number is + i)document.write()Explanation: The loop will break when i=3.(15)try.catchvar txt=function message()try adddlert(Welcome guest!) catch(err) txt=There was an error on this page.nn txt+=Error description: + err.description + nn txt+=Click OK to continue.nn alert(txt)