zoukankan      html  css  js  c++  java
  • JS对象:Number、String

    JavaScript中的对象是有着属性和方法的一种特殊数据类型。 

    常见的对象有数字Number,字符串String,日期Date,数组Array等。 

    Number

    练习

    <style>
    input{
        50px;
    }
    </style>
    
    <script>
    function calc(){
        var n1=document.getElementById("num1").value;
        var n2=document.getElementById("num2").value;
        n1=parseFloat(n1);
        n2=parseFloat(n2);
        var n3=n1*n2;
        var num=new Number(n3);
        document.getElementById("res").value=num.toExponential();
    }
    </script>
    
    <body>
    <input type="text" id="num1" value="5">*
    <input type="text" id="num2" value="6">=
    <input type="text" id="res">
    <input type="button" value="运算" onclick="calc()">
    </body>

    String

    练习

    <script>
    function getValue(id){
       return document.getElementById(id).value;
    }
    function setValue(id,value){
       document.getElementById(id).value=value;
    }
    function replace(){
     
      var src = getValue("src")
     
      var search = getValue("search")
      var replace = getValue("replace")
      var regS = new RegExp(search,"g");
      var result = src.replace(regS, replace);
      setValue("result",result);
    }
     
    </script>
    <table>
    <tr>
      <td>源字符串:</td>
      <td><textarea id="src">example</textarea></td>
    </tr>
    <tr>
      <td>查询:</td>
      <td><input type="text"  id="search" value="a" ></td>
    </tr>
    <tr>
      <td>替换为:</td>
      <td><input type="text"  id="replace" value="e" ></td>
    </tr>
    <tr>
      <td>替换结果:</td>
      <td><textarea id="result"></textarea></td>
    </tr>
    <tr>
      <td colspan="2" align="center"><button onclick="replace()">替换</button></td>
      <td></td>
    </tr>
    </table>
  • 相关阅读:
    .net将动态页面(aspx)生成静态页面htm | html
    js复制Code
    发送Email 附件打包,
    PC大屏自适应 简洁版
    PC大屏自适应
    Java 常见类型转换
    二叉树的四种遍历
    C语言代码质量保证
    System.ComponentModel.Component入门
    旧版MFC自定义CFileDialog Win7中扩展无显示 解决
  • 原文地址:https://www.cnblogs.com/exciting/p/10639311.html
Copyright © 2011-2022 走看看