在html页面引用
<script type ="text/javascript" src ="javascript/hello.js" > </script >
<script type ="text/javascript" > alert("hello world! I'm JavaScript..." ); </script >
注意for里定义变量用var,不是int
<head >
<meta charset ="UTF-8" >
<title > js demo</title >
<style type ="text/css" >
table ,table td {
border :dotted 1 px orange ;
width :200 px ;
}
</style >
</head >
<body >
<script type ="text/javascript" >
document.write("<table>" );
document.write("<tr>" );
for (var x=0 ;x<5 ;x++)document.write("<td>" +x+"</td>" );
document.write("</tr>" );
document.write("<br />" );
document.write("<tr>" );
for (var x=0 ;x<5 ;x++)document.write("<td>" +x+"</td>" );
document.write("</tr>" );
document.write("</table>" );
</script >
</body >
函数
<script >
alert("hello world" );
function show ()
{
document.write("function show" +"<br />" );
return 4 ;
}
show();
function show2 (x,y)
{
document.write("x+y=" +(x+y)+"<br />" );
}
show2(3 ,5 );
function show3 ()
{
for (var x=0 ;x<arguments .length;x++)
document.write(arguments [x]);
}
var z=show;
show3(4 ,5 ,2 ,7 );
document.write("<br />" );
var show4=new Function ("x,y" ,"var sum=x+y;return sum;" );
var answer=show4(9 ,10 );
document.write(answer+"<br />" );
var show5=function ()
{
document.write("我是匿名函数<br />" );
}
show5();
var t=0 ;
function show6 (t)
{
t=1 ;
}
show6(t);
document.write(t);
</script >
数组
<script type ="text/javascript" >
var array1=[1 ,2 ,3 ];
document.write(array1.length+"<br />" );
var array2=new Array (5 );
document.write(array2.length+"<br />" );
var array3=new Array (1 ,2 ,3 );
document.write(array3[0 ]+"<br />" );
</script >
原型
<script type="text/javascript" >
/*原型 为String原型添加一个新功能属性newfunc*/
String.prototype.newfunc = function ()
{
//...
}
</script>