当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JavaScript对象

JavaScript对象

2019年10月25日  | 移动技术网IT编程  | 我要评论

数组对象使用实例

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>array数组应用示例</title>
    <script type="text/javascript">
        var fruit=new array("苹果","梨子","橙子");
        var fruit1=new array("pear1","apple","orange");
        with(document){
            //push()在数组尾部添加数组元素,splice(m,n)删除在m位置的n个元素
            write("<ol>");
            write("<li>",fruit);
            write("<li>",fruit.join());
            write("<li>",fruit.join("、"));//将数组内各个元素以分隔符连接成一个字符串,默认按逗号连接
            write("<li>",fruit.reverse());//reverse倒序数组对象
            write("<li>",fruit.concat(fruit1));//concat()方法用于连接两个或多个数组
            write("<li>",fruit1.sort());//sort按字典顺序对数组元素重新排版
            write("</ol>");
        }
    </script>
</head>

<body>
</body>
</html>

运行结果:

 

 string对象使用实例

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>string对象应用示例</title>
    <script type="text/javascript">
        var str=new string("this is a test string");
        var firstindex=str.indexof("s");
        var lastindex=str.lastindexof("s");
        var arr=new array();
        arr=str.split(" ");
        len=str.length;
        result="第一个's'的位置是:"+firstindex+"<br>最后一个's'的位置是:"+lastindex+"<br>字符串对象的长度是:"+len+"<br>数组arr中的各个元素为:"+"<br>"+arr[0]+"<br>"+arr[1]+"<br>"+arr[2]+"<br>"+arr[3]+"<br>"+arr[4];
        document.write(result);
        document.write("<br><br>字符串对象的字符颜色显示为'红色':"+str.fontcolor("red"));
    </script>
</head>

<body>
</body>
</html>

运行结果:

 

 math对象使用实例

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>math对象应用示例-随机排版图片</title>
    <style type="text/css">
    /*以下是自适应屏幕宽度*/
        div{float: left;height: 160px;width: 160px;margin: 5px;padding: 0}
        div img{border: 0;}
        div a{display: block;padding: 35px 12px}
        div.vbg a{display: block;padding: 10px 35px 15px;}
        div.dispcenter{position: relative;margin: 0 auto;width: 700px;padding: 0 50px 0 70px;background: none;float: none}
    </style>
    <script type="text/javascript" language="javascript">
        document.write("<div class='dispcenter'>");
        var picarr=new array();
        var m=0;
        for(i=1;i<=20;i++){
            if(i<10)
                i="0"+i;
            picarr.push(i);
        }
        while(picarr.length>0){
            i=math.floor(math.random()*(picarr.length-1));
            if(picarr[i]=="03"||picarr[i]=="04"||picarr[i]=="06"||picarr[i]==11||picarr[i]==14||picarr[i]==17||picarr[i]==19||picarr[i]==20){
                document.write("<div class='vbg'><ahref='photo/"+picarr[i]+".jpg'><img srt='photo/thumb/"+picarr[i]+".jpg' /></a></div>");
            }
            picarr.splice(i,1);
        }
        document.write("</div>");
    </script>
</head>

<body>
</body>
</html>

运行结果:未插入图片,请看主要代码

date对象使用实例

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>date对象应用示例</title>
    <script type="text/javascript">
        var today=new date();
        var year=today.getfullyear();
        var month=today.getmonth()+1;
        var date=today.getdate();
        var day=today.getday();
        var dayname=new array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
        var hour=today.gethours();
        var minute=today.getminutes();
        var second=today.getseconds();
        hour=(hour<10)?"0"+hour:hour;
        minute=(minute<10)?"0"+minute:minute;
        second=(second<10)?"0"+second:second;
        var time=hour+":"+minute+":"+second;
        document.write("现在的时间是"+year+"年"+month+"月"+date+"日"+time+" "+dayname[day]);
    </script>
</head>

<body>
</body>
</html>

运行结果:

 

 -------------------------------------------------------------------------

以下为浏览器对象:

创建警告对话框

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>警告对话框创建示例</title>
    <script type="text/javascript">
        document.title="警告对话框创建示例";
        function fun(testobject){
            if(testobject.value.length<6)
                alert("密码长度不得小于6位!");
        }
    </script>
</head>

<body>
    <form>
        请输入密码:
        <input type="password" name="pwd" onblur="fun(this);">
    </form>
</body>
</html>

运行结果:

 

 创建确认对话框

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>创建确认对话框示例</title>
    <script type="text/javascript">
        function isconfirm(){
            if(confirm("你确认删除此信息吗?"))
                alert("信息已成功删除!");
            else
                alert("你取消了删除!");
        }
    </script>
</head>

<body>
    <form method="post" name="form1">
        <input type="button" value="删除" onclick="isconfirm()">
    </form>
</body>
</html>

运行结果:

 

 创建信息提示对话框

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>创建信息提示对话框示例</title>
    <script type="text/javascript">
        var name=prompt("请输入你的姓名","刘先生");
        document.write("你的名字是:"+name);
    </script>
</head>

<body>
</body>
</html>

运行结果:

 

 使用open()打开窗口

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>打开指定窗口(使用open()打开窗口示例)</title>
    <script language="javascript">
        var newwin=window.opener("date对象应用示例.html","temp","left=50,top=120,"+"width=360,heigth=100,resizable=1");
    </script>
</head>

<body>
</body>
</html>

运行结果:请看主要代码

setinterval定时器应用实例

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>setinterval定时器应用示例</title>
    <script language="javascript">
        var sec=0;
        timerid=setinterval("count()",1000);
        function count(){
            num.innerhtml=sec++;
        }
    </script>
</head>

<body>
    停留时间:<font id="num" face="impact" color="red" size="7">0</font>
    秒钟:<input type="button" value="停止" onclick="clearinterval(timerid)">
</body>
</html>

运行结果:

 

 settimeout定时器使用

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>settimeout定时器应用示例</title>
    <style type="text/css">
        div{font-size: 20px;font-weight: bold;font-family: constantia, "lucida bright", "dejavu serif", georgia, "serif";}
    </style>
    <script type="text/javascript">
        msg=new array();//存放显示文字数组
        msg.push("______");
        msg.push("你知道吗?");
        msg.push("______");
        msg.push("金庸写的14本书可以连成一个对联:");
        msg.push("______");
        msg.push("“飞雪连天射白鹿,笑书神侠倚碧鸳”");
        msg.push("______");
        msg.push("j.k.罗琳写的7本书也可以连成一句话:");
        msg.push("______");
        msg.push("“哈哈哈哈哈哈哈”");
        for(i=0;i<msg.length/2;i++){
            document.write("<div></div>");
        }
        interval=100;//定时器运行时间
        seq=0;//显示文字的位置
        i=0;//文字数组下标
        j=0;//div数组下标
        function scroll(){
            if(i%2==0)
                document.getelementsbytagname("div").item(j).innerhtml=msg[i].substring(seq,seq+1)
            else
                document.getelementsbytagname("div").item(j).innerhtml=msg[i].substring(0,seq+1);
            seq++;
            if(seq>=msg[i].length){
                seq=0;
                i++;
                if(i%2==0)
                    j++;//要显示文字才换行
                interval=900;
            }
            if(i>=msg.length){
                i=0;
                j=0;
                settimeout("cleartext()",2000);//文字全部显示后停2s再清空
                return;//退出scroll()函数
            }
            timerscroll=settimeout("scroll()",interval);
            interval=100;
        }
        //清空文本
        function cleartext(){
            for(k=0;k<(document.getelementsbytagname("div").length);k++){
                document.getelementsbytagname("div").item(k).innerhtml="";
            }
            interval=100;
            scroll();
        }
        
        
    </script>
</head>

<body onload="scroll()" style="margin: 0 80px">
</body>
</html>

运行结果:

 

 location对象应用实例

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>location对象应用实例</title>
    <script type="text/javascript">
        function currlocation(){
            alert(window.location);
        }
        function newlocation(){
            window.location="setinterval定时器应用示例.html";
        }
    </script>
</head>

<body>
    <input type="button" onclick="currlocation()" value="显示当前的url">
    <input type="button" onclick="newlocation()" value="改变url">
</body>
</html>

运行结果:

 

 document对象应用实例

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>document对象应用实例</title>
    <script>
        document.title="document对象应用示例";
        var update_date=document.lastmodified;
        document.write("本网页最后更新的时间是:"+update_date);
    </script>
</head>

<body>
</body>
</html>

运行结果:

 

 使用image对象实现鼠标移入时更换显示图片

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>使用image对象实现鼠标移入移出时更换显示图片</title>
</head>

<body>
    <img name="myimage" src="350724200004263015.jpg";
         onmouseover="document.myimage.src='file:///f:/dw/%e7%ab%99%e7%82%b9/350724200004263015.jpg';"
         onmouseout="document.myimage.src='file:///f:/dw/%e7%ab%99%e7%82%b9/350724200004263015.jpg';">
</body>
</html>

运行结果:请看主要代码

form对象(使用表单对象创建注册页面)

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>使用表单对象创建注册页面</title>
    <style type="text/css">
        .insize{height: 120px;width: 600px;font-size: 36px;font-weight: bold}
    </style>
    <script type="text/javascript">
        function check(){
            obj=document.form1;
            name=obj.uname.value;
            if(name=="")
                return;
            if(obj.usex.checked==true){
                sex="男";
                document.getelementbyid("textinfo").style.csstext="background-color:#000;color:#fff;";
            }
            else{
                sex="女";
                document.getelementbyid("textinfo").style.csstext="background-color:#ff9900;color:#000;";
            }
            index=obj.city.selectedindex;
            selcity=obj.city.options[index].text;
            ah=new array();
            for(i=4;i<=8;i++){
                if(obj.elements[i].checked==true)
                    ah.push(obj.elements[i].value);
            }
            if(ah==""){
                alert("请选择一项爱好!");
                return;
            }
            str="您所填写的信息:\n";
            str+="您的姓名是:"+name;
            str+=", "+sex;
            str+=",居住在"+selcity;
            str+="。您喜欢"+ah+"。";
            document.getelementbyid("textinfo").value=str;
        }
        function getelements(){
            num=document.forms[0].length;
            num="一共用了 "+num+" 个表单元素";
            alert(num);
        }
        function setfocus(){
            document.getelementbyid("uname").focus();
        }
    </script>
</head>

<body onload="setfocus()">
    请填写个人信息:<br /><br />
    <div style="border-style: solid;border-color: #f00;border-width: 1px;width: 600px">
        <form id="form1" name="form1" method="post" action="javascript:check()">
            <label>姓名:
                <input type="text" name="uname" id="uname" />
                <br /><br />
            </label>
            性别:
            <label>
                <input name="radio" type="radio" id="usex" value="usex" checked="checked" />
                男
            </label>
            <label>
                <input type="radio" name="radio" id="usex2" value="usex" />
                女
            </label>
            <br /><br />
            <label>居住城市:
                <select name="city" id="city">
                    <option>北京</option>
                    <option>上海</option>
                    <option selected="selected">广州</option>
                    <option>沈阳</option>
                    <option>长春</option>
                    <option>哈尔滨</option>
                </select>
            </label>
            <br /><br />
            爱好:
            <label>
                <input type="checkbox" name="zq" id="zq" value="足球" />
                足球
            </label>
            <label>
                <input type="checkbox" name="yy" id="yy" value="音乐" />
                音乐
            </label>
            <label>
                <input type="checkbox" name="ly" id="ly" value="旅游" />
                旅游
            </label>
            <label>
                <input type="checkbox" name="yx" id="yx" value="游戏" />
                游戏
                <input type="checkbox" name="qt" id="qt" value="其他" />
                其他<br /><br />
            </label>
            <input type="submit" name="button" id="button" value="提交" />
            <input type="reset" name="button2" id="button2" value="重置" />
        </form></div><br />
        <textarea name="textinfo" cols="50" rows="4" readonly="readonly" class="insize" id="textinfo"></textarea>
        <br /><br />
        <input type="button" name="button3" id="button3" value="表单所用的元素个数" onclick="getelements()"/>
</body>
</html>

运行结果:

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网