1,點擊目標區域之外隱藏,運用場景:點擊遮罩層,彈層關閉。 javascript
// 點擊目標區域之外隱藏 $(document).on("click", function (event) { var e = window.event || e; obj = $(e.srcElement || e.target); if ($(obj).is(selector)) { // console.log('內部區域'); // do something } else { // console.log('你的點擊不在目標區域'); // do something } });
2,JS超出顯示省略號java
function getStrLen(str, len) { if(!str) return; var iCount = 0; var reg = /[^\x00-\xff]/; var result = ""; for (var i = 0; i < len; i++) { if (reg.test(str.charAt(i))) { iCount++ } result += str.charAt(i); iCount++ } if (result.length < str.length) { result += "..."; } return result }
三、JS日期加月計算;orm
/** * 日期加月計算 * num:要加的月數 * sdate:要計算的日期,日期對象 */ function addMonth(num,sdate) { //日期不傳默認爲當前日期 var d = sdate || new Date(); //當前月份 var oldMonth = d.getMonth(); //當前幾號 var oldDay = d.getDate(); //每個月多少天,平年 var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; //閏年二月爲29天 var fullYear = d.getFullYear(); if ((fullYear % 4 == 0 && fullYear % 100 != 0) || fullYear % 400 == 0) { days[1] = 29; } //加月,設置月爲:當前月份+要加的月數 d.setMonth(oldMonth + num); //計算月日 var newDay = d.getDate(); if (oldDay == days[oldMonth]) { if(newDay != oldDay){ //設置新日期爲:新日期的上個月的最後一天 d.setDate(0); }else{ //設置爲當月最後一天 d.setDate(1); d.setMonth(d.getMonth()+1); d.setDate(0); } } //輸出年月日,月日不足10前面補0 var y = d.getFullYear(); var m = d.getMonth() + 1; var dd = d.getDate(); if (m < 10) { m = '0' + m; } if (dd < 10) { dd = '0' + dd; } return y + "-" + m + "-" + dd; } addMonth(2,new Date("2017-02-28")) // 2017-04-30
四、一個頁面應用加個倒計時對象
var addTimer = function () { var list = [], interval; return function(id, time) { if (!interval) interval = setInterval(go, 1000); list.push({ ele: id, time: time }); } function go() { for (var i = 0; i < list.length; i++) { document.getElementById(list[i].ele).innerHTML = getTimerString(list[i].time ? list[i].time -= 1 : 0); if (!list[i].time) list.splice(i--, 1); } } function getTimerString(time) { var end = new Date(time); var now = new Date(); var m = Math.round((end - now) / 1000); var day = parseInt(m / 24 / 3600); var hours = parseInt((m % (3600 * 24)) / 3600); var minutes = parseInt((m % 3600) / 60); var seconds = m % 60; return "還有" + day + "天" + hours + "小時" + minutes + "分" + seconds + "秒"; } }(); // 調用 addTimer(id, timestamp);
五、js實現某年某月某天距離如今有多少天,多少分,多少秒,動態顯示!blog
/** * @Author Jone * @DateTime 2019-04-04 * @param {[type]} date [js實現某年某月某天距離如今有多少天,多少分,多少秒,動態顯示!] * @return {[type]} [description] */ function aa(date) { var current = Date(); var seconds = (Date.parse(current) - Date.parse(date)) / 1000; var days = Math.floor(seconds / (3600 * 24)); seconds = seconds % (3600 * 24); var hours = Math.floor(seconds / 3600); seconds = seconds % 3600; var minutes = Math.floor(seconds / 60); seconds = seconds % 60; var formatNumber = n => n.toString()[1] ? n.toString() : '0' + n.toString(); var result = days + '天' + formatNumber(hours) + '小時' + formatNumber(minutes) + '分鐘' + formatNumber(seconds) + '秒'; return result; } // 調用 setInterval(() => { console.log(aa(time)); }, 1000)