zoukankan      html  css  js  c++  java
  • javascript技巧点滴

       在开发网站过程中用到了javascript,特记录其中的一些有价值的技巧以备查询。

    1. toFixed() 将数据四舍五入转换成制定位数的浮点数。如:

        var num = 3.766;    document.write(num.toFixed(2));    结果为3.77。网络上有介绍,toFixed() 对数据的四舍五入不是特别稳定。

    所以在使用时需要注意。重写toFixed()方法

        Number.prototype.toFixed = function( s)
        {

            if(this >=0)

           {
        
               return (parseInt(this * Math.pow( 10, s) + 0.5)/Math.pow(10,s)).toString();

           }

                        else if(this<0)

           {

              return (parseInt(this * Math.pow( 10, s) - 0.5)/Math.pow(10,s)).toString();

           }
        }

    2. 如果需要使某个控件不可用可使用disabled属性 例如:

        <input type="text" id="tt" />  //定义一个id为tt 的文本框

        document.getElementById('tt').disabled =true;  //使该文本框不可用

        document.getElementById('tt').disabled =false;  //使该文本框恢复可用

    3.  文本框的赋值使用 .value 属性,<label id="dw"></label>  标签则要使用.innerHTML属性 例如:

          document.getElementById('dw').innerHTML = '查询预付费类型';

    4.  使用ajax向后台页面传递参数时,如果参数有中文那么中文到后台会变成乱码,所有需要在发送请求时

       将中文用encodeURIComponent()处理一下。

    5  javascript中将字符串转换成日期类型 字符串转换成日期形式

    将字符串形式的日期转换成日期对象

    var strTime="2011-04-16"; //字符串日期格式           
    var date= new Date(Date.parse(strTime.replace(/-/g,   "/"))); //转换成Data();

    var month=date.getMonth()+1; //获取当前月份

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

    date.getYear();        //获取当前年份(2位)
    date.getFullYear();    //获取完整的年份(4位,1970-????)
    date.getMonth();       //获取当前月份(0-11,0代表1月)
    date.getDate();        //获取当前日(1-31)
    date.getDay();         //获取当前星期X(0-6,0代表星期天)
    date.getTime();        //获取当前时间(从1970.1.1开始的毫秒数)
    date.getHours();       //获取当前小时数(0-23)
    date.getMinutes();     //获取当前分钟数(0-59)
    date.getSeconds();     //获取当前秒数(0-59)
    date.getMilliseconds();    //获取当前毫秒数(0-999)
    date.toLocaleDateString();     //获取当前日期
    var mytime=date.toLocaleTimeString();     //获取当前时间
    date.toLocaleString( );        //获取日期与时间

  • 相关阅读:
    【leetcode】106. Construct Binary Tree from Inorder and Postorder Traversal
    【leetcode】105. Construct Binary Tree from Preorder and Inorder Traversal
    【leetcode】236. Lowest Common Ancestor of a Binary Tree
    【leetcode】235. Lowest Common Ancestor of a Binary Search Tree
    【leetcode】352. Data Stream as Disjoint Intervals
    【leetcode】897. Increasing Order Search Tree
    【leetcode】900. RLE Iterator
    BEC listen and translation exercise 26
    BEC listen and translation exercise 25
    BEC listen and translation exercise 24
  • 原文地址:https://www.cnblogs.com/sunleinote/p/2220564.html
Copyright © 2011-2022 走看看