javascript权威指南例子(自己写的)

javascript权威指南例子(自己边看边写的) 前面几章的  下载

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
</HEAD>
<BODY>

<!--sample 1 -->
<script language="javascript">
document.write(
"<h1>hello this is Factorials</h1>")
for (i=0;i<10;i++)
{
    document.write(i
+"*"+i+"="+i*i);
    document.write(
"<br />")
}

</script>


<!--sample 1 一般的事件 -->
<input type="button" value="click here (eventhander)" onclick="alert('you clicked this button!');"></input>


<!--sample 2  函数调用 无返回值-->
<br /> <h3>sample 2  函数调用 无返回值</h3><br />
<script language="javascript">
function function1()
{

alert(
"good");alert("yes");  //语句在不同的行中可以不写分号,建议只要是语句都要写分号。
alert("1")
alert(
"2")
/*
注释

*/

}

</script>
<br />
<input type="button" value="function1" onclick="function1();"></input>

<!--sample 3  求平方根-->
<br /> <h3>sample 3  求平方根</h3><br />

<script language="javascript">
function countsqrt()
{
    result
=Math.sqrt(document.getElementById("numsqrt").value);    //Math.sqrt 计算平方根
    document.getElementById("numsqrt").value=result;    //把计算结果赋值给文本框
    alert(result);
    
//alert((255).toString(0x10))  
}


</script>
<input type="text" id="numsqrt" value="2" ></input>
<input type="button" id="countsqrt" value="计算平方根" onclick="countsqrt();" ></input>

<!--sample 4  特殊数值的常量-->
<script language="javascript">
    document.write(
"<br /> <h3>sample 4 特殊数值的常量</h3><br />")
    document.write(
"Number.MAX_VALUE: "+Number.MAX_VALUE+"<br />");
    document.write(
"Number.MIN_VALUE: "+Number.MIN_VALUE+"<br />");
    document.write(
"Number.MIN_VALUE: "+Number.MIN_VALUE+"<br />");
    document.write(
"Number.NaN "+Number.NaN+"<br />");
    document.write(
"Number.POSTIVE_INFINITY "+Number.POSTIVE_INFINTITY+"\n\r\f1111<br />");
</script>

<!--sample 5 字符串常用操作-->
<script language="javascript">
    document.write(
"<br /> <h3>sample 5 字符串常用操作</h3><br />")
    msg
="wuyisky";
    document.write(msg.length);

    function StringOperate()
    
{
        var index 
= document.getElementById("Operate").value;
        var word 
= document.getElementById("stringInput").value;
        
switch (index)
        
{
            
case "getLength" :
                alert(
"\""+word+"\" 的长度是:  "+word.length);
                
break;
            
case "getLastChar" :
                alert(
"\""+word+"\" 的最后一个字符是:  "+word.charAt(word.length-1));  //charAt:返回指定索引位置处的字符。strObj.charAt(index); strObj 是string类型,index: from 0 to strObj.length-1
                alert("substring(2)结果是: "+word.substring(2+"  // 从参数的索引位置开始到结尾的子字符串");
                alert(
"word.substring(2,4)结果是: "+word.substring(2,4+"  // 从参数1的索引位置开始到参数2的索引位置结束");
                alert(
"word.substr(2,4)结果是: "+word.substr(2,4+"  // 从参数1的索引位置开始的参数2个字符");
                alert(
"word.slice(2,4)结果是: "+word.slice(2,4+"  // 从参数1的索引位置开始的参数2个字符");
                
break;
            
default :
                alert(word);
        }

            
    }

</script>
<input type="text" id="stringInput"  value="被操作的字符串"></input>
<select id="Operate" onchange="StringOperate();">
    
<option value="getLength">求长度</option>
    
<option value="getLastChar">取最后一个字符</option>
    
<option value="2">求长度</option>
    
<option value="3">求长度</option>

</select>
    
<!--sample 6  函数直接量-->
<script language="javascript">
    document.write(
"<br /> <h3>sample 6 函数直接量</h3><br />")
    var f
=function(x) {return x};
    var s
=f("函数直接量;")
    document.write(s);
    
</script>

    
<!--sample 7  对象直接量-->
<script language="javascript">
    document.write(
"<br /> <h3>sample 7 对象直接量</h3><br />")
    var point 
= new Object();    //create a Object
    point.x=2;        //属性x
    point.y=3;        //属性y
    document.write(point.x.toString()+point.y.toString());
    var point1
={x:2,y:3};    //对象直接量
    document.write(point1.x.toString()+point1.y.toString());
</script>



    
<!--sample 8 数组的使用-->
<script language="javascript">
    document.write(
"<br /> <h3>sample 8 数组的使用</h3><br />")
    var a 
= new Array();
    a[
0= 1;
    a[
1= "wuyi";
    a[
2= true;
    a[
3= {x:1,y:"a string"};
    
for (i =0;i<=3;i++)
    
{
        document.write(
"<br />"+a[i]);
    }

    document.write(a[
3].x+a[3].y);
</script>

<!--sample 9 数组的使用-->
<br /> <h3>sample 9 函数中声明的所有变量在整个函数中都有定义</h3><br />


</BODY>
</HTML>

posted on 2007-10-03 14:17  无意  阅读(909)  评论(1编辑  收藏  举报

导航