您的位置:首页 > Web前端 > JavaScript

JavaScript基本语法

2010-08-20 17:25 435 查看
创建脚本块
<scriptlanguage=”JavaScript”>
JavaScriptcodegoeshere
</script>
隐藏脚本代码

<scriptlanguage=”JavaScript”>
<!--
document.write(“Hello”);
//-->
</script>

浏览器不支持的时候显示

<noscript>
Hellotothenon-JavaScriptbrowser.
</noscript>

链接外部脚本文件

<scriptlanguage=”JavaScript”src="/”youname.js"”></script>

注释脚本

//Thisisacomment
document.write(“Hello”);//Thisisacomment
/*
Allofthis
isacomment
*/

输出到浏览器

document.write(“<strong>输出内容</strong>”);

定义变量

varmyVariable=“somevalue”;

字符串相加

varmyString=“String1”+“String2”;

字符串搜索

<scriptlanguage=”JavaScript”>
<!--
varmyVariable=“Hellothere”;
vartherePlace=myVariable.search(“there”);
document.write(therePlace);
//-->
</script>

字符串替换

thisVar.replace(“Monday”,”Friday”);

格式化字串

<scriptlanguage=”JavaScript”>
<!--
varmyVariable=“Hellothere”;
document.write(myVariable.big()+“<br>”);
document.write(myVariable.blink()+“<br>”);
document.write(myVariable.bold()+“<br>”);
document.write(myVariable.fixed()+“<br>”);
document.write(myVariable.fontcolor(“red”)+“<br>”);
document.write(myVariable.fontsize(“18pt”)+“<br>”);
document.write(myVariable.italics()+“<br>”);
document.write(myVariable.small()+“<br>”);
document.write(myVariable.strike()+“<br>”);
document.write(myVariable.sub()+“<br>”);
document.write(myVariable.sup()+“<br>”);
document.write(myVariable.toLowerCase()+“<br>”);
document.write(myVariable.toUpperCase()+“<br>”);
varfirstString=“MyString”;
varfinalString=firstString.bold().toLowerCase().fontcolor(“red”);
//-->
</script>

创建数组

<scriptlanguage=”JavaScript”>
<!--
varmyArray=newArray(5);
myArray[0]=“FirstEntry”;
myArray[1]=“SecondEntry”;
myArray[2]=“ThirdEntry”;
myArray[3]=“FourthEntry”;
myArray[4]=“FifthEntry”;
varanotherArray=newArray(“FirstEntry”,”SecondEntry”,”ThirdEntry”,”FourthEntry”,”FifthEntry”);
//-->
</script>

数组排序

<scriptlanguage=”JavaScript”>
<!--
varmyArray=newArray(5);
myArray[0]=“z”;
myArray[1]=“c”;
myArray[2]=“d”;
myArray[3]=“a”;
myArray[4]=“q”;
document.write(myArray.sort());
//-->
</script>

分割字符串

<scriptlanguage=”JavaScript”>
<!--
varmyVariable=“a,b,c,d”;
varstringArray=myVariable.split(“,”);
document.write(stringArray[0]);
document.write(stringArray[1]);
document.write(stringArray[2]);
document.write(stringArray[3]);
//-->
</script>

弹出警告信息

<scriptlanguage=”JavaScript”>
<!--
window.alert(“Hello”);
//-->
</script>

弹出确认框

<scriptlanguage=”JavaScript”>
<!--
varresult=window.confirm(“ClickOKtocontinue”);
//-->
</script>

定义函数

<scriptlanguage=”JavaScript”>
<!--
functionmultiple(number1,number2){
varresult=number1*number2;
returnresult;
}
//-->
</script>

调用JS函数

<ahref=”#”onClick=”functionName()”>Linktext</a>
<ahref="/”javascript:functionName"()”>Linktext</a>

在页面加载完成后执行函数

<bodyonLoad=”functionName();”>
Bodyofthepage
</body>

条件判断

<script>
<!--
varuserChoice=window.confirm(“ChooseOKorCancel”);
varresult=(userChoice==true)?“OK”:“Cancel”;
document.write(result);
//-->
</script>

指定次数循环

<script>
<!--
varmyArray=newArray(3);
myArray[0]=“Item0”;
myArray[1]=“Item1”;
myArray[2]=“Item2”;
for(i=0;i<myArray.length;i++){
document.write(myArray+“<br>”);
}
//-->
</script>

设定将来执行

<script>
<!--
functionhello(){
window.alert(“Hello”);
}
window.setTimeout(“hello()”,5000);
//-->
</script>

定时执行函数

<script>
<!--
functionhello(){
window.alert(“Hello”);
window.setTimeout(“hello()”,5000);
}
window.setTimeout(“hello()”,5000);
//-->
</script>

取消定时执行

<script>
<!--
functionhello(){
window.alert(“Hello”);
}
varmyTimeout=window.setTimeout(“hello()”,5000);
window.clearTimeout(myTimeout);
//-->
</script>

在页面卸载时候执行函数
<bodyonUnload=”functionName();”>
Bodyofthepage
</body>


本文出自 “博客即日起停止更新” 博客,请务必保留此出处http://sucre.blog.51cto.com/1084905/380691
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: