js一些常规操作

1.判断数组为空

var arrayList = []

方法1. 
if (arrayList == (null || "" || undifine)) {
    为空操作
}

方法2.
if (arrayList.length > 0) {
    非空操作
}

 

2. 数组splice() 的用法

  ①删除    splice(index, num)                       index - 删除起始位置       num - 删除的个数

    var arrayList = ['a', 'b', 'c', 'd', 'e'];
    var newList = arrayList.splice(0, 1);
    alert(arratList);//['a','b','c','d','e']
    alert(newList);//['b','c','d','e']

  ②添加    splice(index, 0, insertValue)        index - 添加的起始位置    insertValue - 添加的项

     var arrayList = ['a', 'b', 'c', 'd', 'e'];
     var newList = arrayList.splice(0, 0, 'tom');
     alert(arratList);//['a','b','c','d','e']
     alert(newList);//['tom','a','b','c','d','e']

  ③替换    splice(index, num, insertValue)    index - 替换的起始位置    num - 替换掉的个数   insertValue - 替换的项

    var arrayList = ['a', 'b', 'c', 'd', 'e'];
    var newList = arrayList.splice(0, 1, 'tom');
    alert(arratList);//['a','b','c','d','e']
    alert(newList);//['tom','b','c','d','e']

 

3. 字符串添加字符 slice

  其中 soure 为原字符串, start 为将要插入字符的位置,newStr 为要插入的字符

function insertStr(soure, start, newStr){   
   return soure.slice(0, start) + newStr + soure.slice(start);
}

  示例:

function insertStr(soure, start, newStr){   
   return soure.slice(0, start) + newStr + soure.slice(start);
}

var time = 201904111005; //原字符串

var newTime = insertStr(insertStr(insertStr(insertStr(insertStr(time,4,"/"),7,"/"),10,"  "),14,":"),17,":00");

alert(_NewOccurTime ); //2019/04/11 10:05:00

 

4. 问题:前台的时间显示发生变化,和数据库不一致

  前台:Sat Nov 01 00:00:00 CST 1975

  <input th:value="${listTeacher.birthday}" >

    方法:进行格式化

    <input th:value="${{#dates.format(listTeacher.birthday,'yyyy-MM-dd')}">

    结果:1975-11-01

 

5.jQuery 的 hide() 方法不起作用

$("#accountmodal").hide();

原因:在文档未加载完时就已经加载了

方法:

$(document).ready(function() {
           $("#accountmodal").hide();
        })

 

6.改变 <li> 的数量及内容

$("ul").each(function (i, dom) {
                    var dom = $(dom);
                    for (var j = 0; j <= 8; j++) {
                        dom.append("<li>");
                        $("li").eq(j).html(j)
                    }
                });

 

posted @ 2018-11-09 10:01  GGGG6666  阅读(232)  评论(0编辑  收藏  举报