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

WBOY
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: 1)
Text movement//Only numbers can be entered
3) Make the selected row of the table Different colors appear
4) Prevent users from entering values ​​into the text box:
onkeypress="reuturn false"
Note: In any event of the form element, as long as return false is added, this will not be triggered. Event
25: Regular expression:
is like the wildcard character in DOS, used to detect whether an input meets a specific wildcard character
^: represents the beginning of a line of characters
$: represents the end of a line of characters
[]: Used to define acceptable characters
[a-z]: Indicates that lowercase letters are accepted
[A-Z]: Indicates that uppercase letters are accepted
[0-9]: Indicates that numbers are accepted
[0-9,_,a]: Indicates that numbers, underscores or letters a can be accepted
[a-zA-Z0-9]: Indicates that both English letters and numbers can be accepted
[ ^]: Unacceptable characters
[^a-z]: Unacceptable English letters
{}: Used to define the number of characters that must be entered
{3}: Must contain 3 characters. {4,8}: At least 4 and at most 8 characters
[0-9]{3}: 3 numbers must be entered
[a-zA-Z]{4,6}: Must be entered 4 to 6 English letters
{n,}: indicates that at least n characters can be entered.
[a-z]{0,}:表示可以接受0个或多个英文字母
[a-z]{1,}:表示可以接受至少1个英文字母
+:匹配前面字符的1次或多次-----相当于{1,}
*:匹配前面字符的0次或多次-----相当于{0,}
.:表示任意字符
举例:
frm1.user.value.match("^[0-9a-zA-z]{5,8}$")
26:如何利用Js对表单元素进行控制:
A:文本框:
1)得到文本框的文本:
表单名.表单元素名.value
2)获得焦点:
表单名.表单元素名.focus()
B:按钮:
1)使按钮不可用:
表单名.按钮名.disabled=true (true,不可用;false 可用)
2)使按钮不可见:
表单名.按钮名.style.display="none" 不可见
表单名.按钮名.style.display="" 可见
if (表单名.按钮名.style.display=="") 如果可见
C)单选框:
得到单选框所选中的值 [各个单选框的名称一定要一样,值不一样]
for(i=0;iif (单选框数组[i].checked) break;
返回 单选框数组[i].value就行了

D):得到所有打勾的复选框的值
for(i=0;iif (复选框数组[i].checked) 执行对应的语句
E):下拉框
A)得到所选中的值-->下拉框.value
B)删除里面的全部内容----->下拉框.length=0;
C)删除里面的指定项--->下拉框.options.remove(下标);
D)往里面添加一项----->下拉框.options[下拉框.length]=new Option("标签","值");
注意下拉框的options是一个数组,用来存储所有选择,下标是从零开始的
E)selectedIndex:得到或设置所选中的项的下标
F)options[k].value:得到第i项的值
G)onchange事件:当选中项发生改变时,触发

举例:在客户端实现两个下拉框的联动[注意定义一维数组]
city=new Array();
city[0]=new Array("湖北省","武汉");
city[1]=new Array("湖北省","仙桃");
city[2]=new Array("湖北省","洪湖");
city[3]=new Array("福建省","广州");
city[4]=new Array("福建省","厦门");
city[5]=new Array("福建省","漳州");
27:创建一个模块的js的文件,然后在页面中来调用
A)直接新建一个*.js文件:
直接写上函数,不用加来引用
28:多个对像共享同一个事件:


第四章:C#.net语法基础
在这一章中,你将要学习以下一些内容
1:编写asp.net语言的选择
2:vs.net 2005 界面技巧
3:如何在页面中加入服务器端代码
4:如何在页面中导入命名空间
5:C#.net语法基础
6:动态的由服务器端向客户端加入javascript


编写asp.net语言的选择:

编写asp.net程序,net为我们提供了以下几种语言vb.net,c#.net,j#.net其中vb.net语言是最简单,最容易学的语言,它继承了vb的大部分语法,同时又加入了一整套.net framework,利用vb.net开发asp.net程序是最容易的一门的语言C#.net是整个.net的核语言,它继承了c,c++的大部分语法,较vb.net有点复杂,但是执行程序的效率比vb.net更高,j#.net是继承了javascript的大部分语法,一般很少用。我们选择C#.net语言作来开发asp.net程序的语言

vs.net 2005 界面技巧
1)设置显示解决方案---------:工具--选项-->项目和解决方案-->常规
2)对单网页可以进行生成,不需要对整个项目进行生成
3)设计模式与源文件模式(html模式),后代码模式(类),让页面一加载时自动显示设计模式
4)文档大纲窗口:可以清楚层现html标签的层次关系:视图-->其它窗口--->文档大纲
5)Html标签导航:切换到源文件模式,单右-->选择最下面的"选中html标签"
6)源文件模式下控件拖曳
7)多文档页面显示------ctrl+tab可以在不同文档之间切换
8)Asp.net网站特殊文件夹:
A:App_Code用来存放代码文件(比如:*.cs,类文件)
B:App_Date用来存放网站数据文件(数据库文件,xml文件等)
C:还有很多其它的特殊文件夹
9)程序代码重构:
A:重构属性
B:重构方法
10)Asp.net网站的动态编译:
A:当asp.net第一次运行时,IIs会自动为asp.net生成一个dll,所以第一次非常慢
以后只要文件的内容的没有发生改变,IIs就会延用上一次生成的dll,不会再次生成
新的dll,所以第一次运行慢,以后运行快
B:如果asp.net的源文件内容一旦发生改变,则IIs会重新生在一个dll,利用这个特点
我们可以在vs.net环境中写程序,而在IIs中直接打开网站后刷新即可

如何在页面中加入服务器端代码
C#.net只能被服务器端的IIS来编译执行,所以C#.net语言是一定要运行于服务器端
A:直接把代码加入"后代码文件"的事件里面[采用CodeBehind]
B:直接把代码加入"页面文件"里面此时一定要加来限制 [采用CodeBeside]
1)如果是单纯的C#代码,可以直接用括起来,并且可以有多个
2)如果是函数,则一定要紧跟在命令符下面,并按照如下的格式:

3)如果要得到一个变量的值,可以写上
比如:
今天是:

如何在页面中导入命名空间
1)在代码文件中:使用using语句
2)在页面文件中:使用
位置在 的下面


C#.net的语法基础
C#.net的数据类型:
Int,Double,String,Char,object数据类型(相当于vb里面的变体类型)
定义变量:
类型标识符 变量名;
给变量赋初值:
可以在定义的时候,给变量赋初值---Int a=5;
也可以在定义之后,给变量赋初值
运算符:
a):赋值运算符:=
b):算术运算符:+,-,*,\(整除),%(余)
c):字符串联接符:+
d):关系运算符:>,>=,e):逻辑运算符:&&,||,!
f):复合运算符:x+=3,x*=6等

asp.net的输入和输出:
a):输出
1):输出单纯的字符串: response.write("字符串");
2):输出html标记:response.write("html标记")[重要重要]
比如:Response.Write("我的链接")
3):输出js脚本:Response.Write("<script>js代码</script>");
4):输出当前的日期和时间:
Response.Write(DateTime.Now.ToLongTimeString()):时间
Response.Write(DateTime.Now.ToLongDateString()):日期
b):输入:利用控件进行输入比如:textbox控件等

if 语句,select case语句
举例:
A)让用户输入两个数和一个操作符,求出运算结果
B)让用户输入三门功课求出平均分和总分并根据求出的结果判断优,良,差
C)根据星期把对应的背景图片换成是"星期"的图片

循环语句:for,while循环
举例:
A)输出Moon1.gif~Moon8.gif这几个文件里面的图片
B)利用表格打印九九乘法表
C)实现掷骰子游戏
7:数组:
1):声明与初始化
静态数组的声明与初始化:
string[] NameList = new string[6];
进行初始化
string[] NameList=new string[5] {"张三","李四","王五","赵六","王七"};
动态数组的初始化:
string[] NameList=new string[] {"张三","李四","王五","赵六","王七"};
2):数组.length:用来读出数组的长度
举例:
A)定义姓名,语文,数学,化学数组,通过函数与表格求出总分与平均分
B)利用数组和表格打印出一些商品的列表清单

8:哈希表(HashTable):是一种两栏数据结构。One column is the key (Key), and the other column is the value (Value)
Create a hash table:
Hashtable has = new Hashtable();
Add data:
has.add("Key" , value)
Get the value of the specified key
has["key"]
Check whether a key already exists
has.ContainsKey("key")
Delete a key
has.Remove("key")
Clear all keys
has.clear();
Access the contents of the hash table:
Hashtable ht = new Hashtable();
foreach ( DictionaryEntry item in ht)

26: Dynamically add client-side javascript to the server (web control) [Important]
Previous methods were based on pre-written functions, and then called to the client , but sometimes, you may need the data from the server
to be processed on the client. At this time, you need to dynamically add code to the client
method:
Write in the Page_Load event:
1:RegisterClientScriptBlock("chen", js) method to register js
2:IsClientScriptBlockRegistered("chen") method to check whether some js have been registered, thus
avoiding a js being registered repeatedly
Example:
1) Read employee information from the database and put it into the js of the client. The form is as follows:
Worker=new Array()
Worker[0]=new Array('Zhang San','BM1');
Worker[1]=new Array('Li Si',' BM1');
Worker[2]=new Array('Wang Wu','BM2');
Worker[3]=new Array('Zhao Liu','BM2');
2 ) realizes the linkage of two drop-down boxes through the database (no refresh)

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