zoukankan      html  css  js  c++  java
  • JS部分代码分享

    2015-10-12 :

    1、search()表达式:搜索索引

    <!DOCTYPE html>
    <html>
    <body>

    <p>搜索字符串 "w3cSchool", 并显示匹配的起始位置:</p>

    <button onclick="myFunction()">点我</button>

    <p id="demo"></p>

    <script>
    function myFunction() {
    var str = "Visit W3cSchool!";
    var n = str.search(/w3cSchool/i);
    //var n = str.search("w3cSchool");//亦能达到同等效果
    document.getElementById("demo").innerHTML = n;
    }
    </script>

    </body>
    </html>

    2、replace()表达式:替换字符串

    <!DOCTYPE html>
    <html>
    <body>

    <p>替换 "microsoft" 为 "W3cSchool" :</p>

    <button onclick="myFunction()">点我</button>

    <p id="demo">Please visit Microsoft!</p>

    <script>
    function myFunction() {
    var str = document.getElementById("demo").innerHTML;
    var txt = str.replace(/microsoft/i,"W3cSchool");//replace("Microsoft", "w3cschool");亦能达到同样效果
    document.getElementById("demo").innerHTML = txt;
    }
    </script>

    </body>
    </html>

    3、使用 test()方法校验

    <!DOCTYPE html>
    <html>
    <body>

    <script>
    var patt1=new RegExp("i");//校验是否包含 'i'

    document.write(patt1.test("The best things in life are free"));验证字符,true或false

    document.write(/e/.exec("The best things in life are free!"));//搜索该字符串,空则返回null
    </script>

    </body>
    </html>


    4、split()分割字符串对象

    <!DOCTYPE html>
    <html>
    <body>

    <p id="demo">Click the button to display the array values after the split.</p>

    <button onclick="myFunction()">Try it</button>

    <script>
    function myFunction()
    {
    var str="How are you doing today?";//要输出的内容
    var n=str.split(",");//使用 str.split(" "); 输出效果为 'How,are,you,doing,today?'
    document.getElementById("demo").innerHTML=n;替换内容
    }
    </script>

    </body>
    </html>

    5、等于('==')和恒等于('===')区别

    <!DOCTYPE html>
    <html>
    <body>

    <p>x 赋值为5 , 显示比较后的值 (x === "5"):</p>

    <button onclick="myFunction()">点我</button>

    <p id="demo"></p>

    <script>
    function myFunction() {
    var x = 5;
    document.getElementById("demo").innerHTML = (x === 5);//值和类型均相符,true
    document.getElementById("demo").innerHTML = (x == '5');//值相符,true
    }
    </script>

    </body>
    </html>


    6、表单验证大全

    原作者 博客园 地址:
    http://www.cnblogs.com/wzmaodong

  • 相关阅读:
    项目Beta冲刺(团队) --1/7
    Beta:凡事预则立
    事后诸葛亮(葫芦娃队)
    项目问题回复
    Alpha冲刺阶段总结
    Alpha冲刺笔记十:第十天
    Alpha冲刺随笔九:第九天
    项目测试用例
    Alpha冲刺随笔八:第八天
    项目Beta冲刺(团队2/7)
  • 原文地址:https://www.cnblogs.com/rick168/p/4870828.html
Copyright © 2011-2022 走看看