zoukankan      html  css  js  c++  java
  • 操作字符串

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
        <title>Document</title>
        <script>
            // window.onload=function()
            // {
            //     function $id(id){return  document.getElementById(id);}
            //     function fn(shubiaoid,zifuchuan)
            //     {
            //         $id(shubiaoid).onclick=function()
            //         {
            //             this.nextSibling.innerHTML=zifuchuan;
            //         }
            //     }
            //     fn("btn1" , $id("div1").innerHTML.concat($id("div2").innerHTML));
            // }
    
            window.onload=function()
            {
                var div1=document.getElementById("div1").innerHTML; //.innerHTML不能丢,是得到盒子里的字符串,字符串之间的连接
                var div2=document.getElementById("div2").innerHTML;
                function fn(shubiaoid,zifuchuan)
                {
                    document.getElementById(shubiaoid).onclick=function()
                    {
                        this.nextSibling.innerHTML=zifuchuan;
                    }
                }
                 fn("btn1",div1.concat(div2));
                 fn("btn2",div1.slice(0,3));//返回:谢谢主,从第0个取到第三个(不包括3)
                 fn("btn3",div1.slice(1));//返回:谢主席,起始位置一定要写,结束位置可以不写,默认取到最后
                 fn("btn4",div1.substr(0,3));//返回:谢谢主,从第0个开始取3个
    
    
    
                //保留小数位数
                var PI=3.1415926;
                var str=PI+"";
                console.log(str.substr(0,str.indexOf(".")+3)); //保留2位小数,indexOf(".") 加引号
                console.log(str.substr(0,str.indexOf(".")+5));//保留4位小数
            }
        </script>
    </head>
    <body>
    <div id="div1">谢谢主席</div>
    <div id="div2">问候在场各位</div>
    <button id="btn1">concat()</button><span>  </span> <br> <!--注意button和span之间不能有空格,否则nextSibling会把空格当成下一个兄弟-->
    <button id="btn2">slice(0,3)</button><span>  </span><br>
    <button id="btn3">slice(1)</button><span></span><br>
    <button id="btn4">substr(0,3)</button><span></span>
    </body>
    </html>
    

      

  • 相关阅读:
    Objective-C之Protocol
    Objective-C之集合对象的内存管理
    IOS的UI基础02
    IOS之UIImageView--小实例项目--带音效的拳皇动画
    IOS的UI基础01
    技术分享(持续更新)
    计算机英语词汇大全
    XCode的安装包校验伪真
    Objective-C之用C的字符来处理NSString相关的字符替换和拼接的问题
    Objective-C之代理设计模式小实例
  • 原文地址:https://www.cnblogs.com/shanlu0000/p/11228709.html
Copyright © 2011-2022 走看看