zoukankan      html  css  js  c++  java
  • js常用写法

    时间戳:

    获取时间戳的方法:
    1、(new Date("2017/04/14")).valueOf()
    2、Date.parse(new Date("2017/04/14"))
    3new Date("2017/04/14").getTime()
    4、+new Date("2017/04/14")
    
    时间戳对比:
    new Date('2016/09/03')的时间为:2016-09-03
    new Date(2016, 9, 3)的时间为:2016-10-03,加了一个月

    判断:

    if(result){
      console.log('aaaa')
    };
    可写成:
    result&&console.log('aaaa')
    
    if(!result){
      console.log('aaaa')
    };
    可写成:
    result||console.log('aaaa')

    条件选择:

    if(result=="yes"){
      a="111";
    }else{
      a="000";
    }
    可写成:
    a=result=="yes"?"111":"000";


    去除数组中的undefind:

    var a=[2,5,undefind,6,8,undefined,9];
    var b=[];
    for(var i=0;i<a.length;i++){
      if(a[i]!=undefined){
        b.push(a[i])
      }
    };
    可写成:
    var a=[2,5,undefind,6,8,undefined,9];
    a=a.filter(function(){
      return true;
    })

    把数组中值的空隔换成其他:

    var data=[2009/10/18 6:00', '2009/10/18 7:00', '2009/10/18 8:00']
    data.map(function (str) {return str.replace(' ', '
    ') });
    console.log(data)

    判断元素是否在数组中:

    var a=[48,56,65,77,6,80,92]; 
    for(var i in a){ 
       if(i===6){ 
          console.log("包含6"); 
       } 
    };
    可写成:
    var a=[48,56,65,77,6,80,92]; 
    a.indexOf(6)>-1 && console.log("包含6");

    return布尔值时:

    (function (){ 
       if (age > 18) {
          return true;
       } else {
          return false;
       }
    })();
    可写成
    (function (){
       return age > 18;
    })();

    if不嵌套if:

    if (user.id === 10) {
       if (user.name !== "") {
          if (user.email === "email") {
             //do something...
          }
       }
    }
    可写成:
    if(user.id === 10 && user.name !=="" && user.email === "email") {
       //do something...
    }

    固定范围内的取值,超出取 最大值|最小值:

    (function(){
      var a=120;
      if(a>100){
        a=100;
      }else if(a<0){
        a=0;
      }
      alert(a);
    }());
    可写成:
    (function(){
      var a=120;
      a=Math.min(100,Math.max(a,0));
      alert(a);
    }())

    取指定范围内的随机数:

    Math.random()*(min-max)+max

    jquery 创建div的方法:

    $('<div>',{
      id:'ddd',
      class:'bbb',
      css:{'300px',height:'200px',backgroundColor:'#333'}
    })

    tab形式的iframe:

    <table cellpadding="0" cellspacing="0">
     <tr>
      <td><a href="a.html" target="fx">aaa</a></td>
      <td><a href="b.html" target="fx">bbb</a></td>
     </tr>
    </table>
    <iframe name="fx" id="fx" src="a.html" width="100px" frameborder="0" scrolling="no"></iframe>    

    防止被Iframe嵌套:

    if(top != self){
        location.href = ”about:blank”;
    }

    用json替换switch:

    (function(){
      var a=1,b;
      switch(a)
      {
        case 1:
          b='苹果';
          break;
        case 2:
          b='桔子';
          break;
        case 3:
          b='香蕉';
          break;
        case 4:
          b='梨子';
          break;
      }
      console.log(b)
    }())
    ==>
    (function(){
      var obj={1:'苹果',2:'桔子',3:'香蕉',4:'梨子'},a=4;
      console.log(obj[a])
    }())

     json排序

    var grades = [
      {
        "name": "张三",
        "grade": 95
      },
      {
        "name": "李四",
        "grade": 62
      },
      {
        "name": "王五",
        "grade": 86
      }
    ];
    grades.sort(function(a, b) { return a.grade - b.grade })

     禁用右键菜单:

    $(document).ready(function(){
      $(document).bind("contextmenu",function(e){
        return false;
      });
    });
  • 相关阅读:
    项目延期原因及应对之道
    我只是来刷屏的
    php学习1留言板的创建
    位运算
    hnu 12264 collisions
    数组和指针的区别
    hnu12263 Gluttonous robot
    解决Mac上安装Zookeeper问题:FAILED TO WRITE PID
    Dubbo问题记录:No provider available for the service xxx from registry localhost:9090
    SqlServer和mysql的日期函数备忘
  • 原文地址:https://www.cnblogs.com/kerry-xu/p/6709107.html
Copyright © 2011-2022 走看看