Home >Web Front-end >JS Tutorial >JavaScript syntax basics. Friends who want to learn js can take a look_basic knowledge
JavaScript syntax basics. Friends who want to learn js can take a look_basic knowledge
WBOYOriginal
2016-05-16 18:39:08969browse
1: JavaScript is case-sensitive 2: Each JavaScript statement must end with ";", the same as C language 3: Output: document.write("string") ---> You can also output the corresponding HTML tag 4: Change the color of the form document.bgColor="red"; 4: Type conversion: parseInt, parseFloat 5: Random function: parseInt(Math.random()*90 10 ) Generate random numbers from 10 to 100 5: Pop up dialog box: alert("prompt content") 5: if if...else, for, while, switch case 5: How Define the array: 1) One-dimensional array: a=new Array(); [No need to specify the length when defining the array] a[0]=1; a[1]=1 ; a[2]=1; s=0; for(i=0;i{ s =a[i]; } 2) Two-dimensional array: city=new Array(); city[0]=new Array("Hubei Province", "Wuhan"); city[1] =new Array("Hubei Province","Xiantao"); city[2]=new Array("Hubei Province","Honghu"); city[3]=new Array("Fujian Province" ,"Guangzhou"); city[4]=new Array("Fujian Province","Xiamen"); city[5]=new Array("Fujian Province","Zhangzhou"); 5: Functions in JavaScript, function calls, and the scope of variables 6: Pop up a query dialog box: confirm ("query content") 7: Close the form: window.opener=null ;window.close(); 8: Open a form: 1) Variable name = window.open ("Webpage name") Open a new window 2) Variable name = window.open( "Webpage name", "name", "height=200px, width=300px") 3) Open the window and pop up in the center of the screen t=window.open('dotest.htm','test',' height=400px,width=500px'); t.moveTo((screen.width-500)/2,(screen.height-400)/2; 4)window.location="url" No A new window will open 5) Pop up in a modal form window.showModalDialog('dotest.htm','','dialogWidth=600px;dialogHeight=500px'); 9: Refresh one Form: window.location.reload(); 10: Get the value of the form element of this form: form name.element name.value 11: How to access it in another form Form element of the previous form A: Modal form: Source form 1) window.showModalDialog('dotest.htm',window,'dialogWidth=600px;dialogHeight=500px') ; Note that the name must be written window 2)window.dialogArguments.form1.txtuser.value B: Non-modal form: Source form: window.open Destination form: window.opener.form name.form element name.value 12: How to return a value to the parent form through a modal form: Source form: t =window.showModalDialog(parameter) alert(t) Destination form: window.returnValue=value;window.opener=null;window.close(); 13: How to close While subform, refresh the parent form A) Non-modal window Source page: window.open("page") Destination page: window.opener.location .href=window.opener.location.href; window.opener=null;window.close(); B) Modal window source page: window.showModalDialog();- ------Use of pause code window.location.reload(); Destination page: window.opener=null;window.close(); 14: Set status Column text: window.status 15) Convert string to numerical value: parseInt("String"), parseFloat("String") 16) Get the current time var date=new Date( ); document.write(date.toLocaleTimeString()); 17): Get the current date: var date=new Date(); document.write(date.toLocaleDateString() ); 18): Return to the previous page.Note that it is not refreshing history.go(-1) 19: Change the background color of an object this.style.backgroundColor='yellow', this.style.color='text Color' 20: Set as homepage: this.style.behavior='url(#default#homepage)';this.setHomePage('your webpage'); 21): Specify the code Automatically execute a process repeatedly after a few minutes. setInterval("js code",1000) Example: Let a background change continuously [the page flashes very badly] var index=1; function ChangePic() { form1.p1.src=index ".jpg"; index=index 1; if (index==4) { index =1; } } setInterval("ChangePic(index)",1000); Improvement: [define the array first and preload the picture in the memory] pic=new Array(4); pic[0]=new Image(); pic[1]=new Image(); pic[2]=new Image(); pic[3 ]=new Image(); pic[0].src="1.jpg"; pic[1].src="2.jpg"; pic[2].src=" 3.jpg"; pic[3].src="4.jpg"; function ClearText() { form1.p1.src=pic[index].src; index=index 1; if (index==4) { index=1; } } setInterval("ClearText(index)",1000); 23): Let the specified code be executed after a certain amount of time, but only once: setTimeout("js code",1000); 24): Clear the text of all text boxes in a form for(i=0;i{ if (form1.elements[i].type=="text") { form1 .elements[i].value=""; } } 25) Run an executable file: obj=new ActiveXObject("wscript.shell"); obj. run("calc.exe"); 26) Events in Java script A)onmouseove: mouse arrives B)onmouseout: mouse leaves event C)onclick: click event D)onKeypress: When the key is pressed, you can get the Asii code of the pressed key through event.keyCode E) load event: writing the code directly in is equivalent to the Load event F)onsubmit :The form submission event will be triggered when the form is submitted Principle: When the user presses the submit button, the onsubmit event of the form will be triggered. In this event, it is determined whether the form needs to be submitted based on the value (true, false) returned by the user. If it is true, it will be submitted, if it is false, it will not be submitted. So we often use a function to perform data verification. Example:
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn