• Javascript数组,String对象,Math对象,Date对象,正则表达式


    标题栏的滚动

    <html>
    <head>
    <title>山西众创金融</title>
    </head>

    function init(){
    //1.拿到标题栏的文本 var title = document.title; //alert(title); //2.将文本字符串转换为数组: var arr = title.split(""); //alert(arr); //3.拿到数组的第一个元素,并从数组中删除 var first = arr.shift(); //4.将第一个元素添加到数组的最后 arr.push(first); //5.将数组再组合成一个字符串 title = arr.join(""); //6.将字符串再赋值回标题栏 document.title = title; //7.每隔一秒做一遍前六步 setTimeout("init()",1000); } </script> <body onload = "init()"> <!--onload事件,从开始就加载--> <hr> </body>

    </html>

    String 对象

    big()

    <html>

    <head>
    String对象
    </head>

    <script type="text/javascript">
    /*
    String对象的方法
    */
    function fun(){
    //1.拿到p标签的对象
    var p = document.getElementById("p");
    //2.拿到p标签对象的主体内容
    var txt = p.innerHTML;
    //alert(txt);
    //innerHTML必须是有开始标签和结束标签的对象才能使用
    //3.改变字体内容,再赋值回去
    p.innerHTML = txt.big().big();
    }
    var arr = ["red","bule","yellow","green","purple"];
    function fun2(){
    //1.拿到p标签对象
    var p = document.getElementById("p");
    //2.随机取得一个整数作为数组下标
    var index = Math.floor(Math.random()*arr.length);
    <!--random是0-1之间的数,floor是小于等于这个数-->
    //3.拿到p标签对象的主体内容
    //var txt = p.innerHTML;
    var txt = p.innerText;
    //alert(txt);
    //4.给p改变颜色,并赋值回去改变颜色,并赋值回去
    p.innerHTML = txt.fontcolor(arr[index]);
    //alert(p.innerHTML);<!--出现问题了,多个标签堆到一起了,所以改成p.innerText-->
    setTimeout("fun2()",2000);
    }

    function fun3(){
    var p = document.getElementById("h1").innerHTML;//标签和文本一起拿到
    alert(p);
    var b = document.getElementById("h1").innerText; //只拿到文本
    alert(b);
    }
    /*
    1.substr() :截取子字符串,两个参数,第一个是下标,第二个是截取的长度。
    2.substring() :截取子字符串,两个参数,代表的是下标
    */
    var s = "abcdefghig";
    alert(s.substring(2,3)); //结果是 c --0,1,2
    alert(s.substring(3,2)); //结果是 c 反过来截取
    alert(s.substr(2,3)); //结果是 cde

    </script>

    <body>
    <p id="p">变大</p>

    <input type="button" value="变大" onclick="fun()">
    <input type="button" value="变色" onclick="fun2()">
    <hr>
    <h1 id="h1"><font>你好</font></h1>
    <input type="button" value="演示两个标签innerHTML和innerText的区别" onclick="fun3()">
    </body>


    Math对象:
    1.floor(x): 取得小于等于x的最大
    2.ceil(x):取得大于等于x的最大整数
    3.random():取得0-1之间的随机数(可以等于0,永远不能取得1)
    4.round():四舍五入为整数

    var a = 3.1;
    alert(Math.floor(a)); //3
    alert(Math.ceil(a));//4
    alert(Math.random());//
    alert(Math.round(a));//3


    Date对象
    1.拿到当前时间
    2.拿取年月日时分秒 getXXX

    var d = new Date();
    alert(d);
    alert(d.toLocaleString());//本地的日期格式
    
    alert(d.getYear()); //显示116 从1900年开始算
    alert(d.getMonth()); //0-11月,所以显示的是11,现在是2016年12月16日,星期五
    alert(d.getDate()); //得到日期:16
    alert(d.getDay());//星期 显示 5

    正则表达式

    var reg = /.../ ; //包含三个任意的字符
    var s = "abcdefg";
    var reg2 = /(..)./;//括号表示子匹配
    
    alert(reg.test(s));//测试字符串中是否包含正则表达式中所匹配的字符串,返回boolean类型
    alert(reg.exec(s)); //以数组的形式返回匹配的正则表达式的字符串
    alert(reg2.exec(s));// abc,ab
    alert(reg2.exec(s)[1]);//ab
  • 相关阅读:
    Python中Linux开发的常识
    Python GUI编程(Tkinter)
    linux文本编辑器教学
    WordCloud安装
    怎么安装wordcloud
    java内功 ---- jvm虚拟机原理总结,侧重于GC
    spring源码分析(二)Aop
    spring源码分析(一)IoC、DI
    java内部类技术提炼
    java自定义注解实现前后台参数校验
  • 原文地址:https://www.cnblogs.com/taiguyiba/p/6184578.html
Copyright © 2020-2023  润新知