定时器轮询监听readyState的状态,若是是 complete 或者 interactive 说明文件加载完成。css
let iframe = document.createElement('iframe'); iframe.src = path; iframe.style.display = 'none'; document.body.appendChild(iframe); const timer = setInterval(() => { const iframeDoc = iframe.contentDocument || iframe.contentWindow.document; if (iframeDoc.readyState == 'complete' || iframeDoc.readyState == 'interactive') { document.body.removeAttribute(iframe); clearInterval(timer); resolve('success'); } }, 1000);
//获取元素 function getElement(ele) { return document.getElementById(ele); } //自动居中函数 function autoCenter(el) { var bodyX = document.documentElement.offsetWidth || document.body.offsetWidth; var bodyY = document.documentElement.offsetHeight || document.body.offsetHeight; var elementX = el.offsetWidth; var elementY = el.offsetHeight; el.style.left = (bodyX - elementX) / 2 + "px"; el.style.top = (bodyY - elementY) / 2 + "px"; }
function getType(obj) { // 为啥不用typeof? typeof没法区分数组和对象 if(Object.prototype.toString.call(obj) == '[object Object]') { return 'Object'; } if(Object.prototype.toString.call(obj) == '[object Array]') { return 'Array'; } return 'nomal'; }; function deepCopy(obj) { if (getType(obj) == 'nomal') { return obj; } else { var newObj = getType(obj) == 'Object' ? {} : []; for(var key in obj) { // 为啥要用hasOwnProperty?不须要从对象的原型链上进行复制 if(obj.hasOwnProperty(key)) { newObj[key] = deepCopy(obj[key]); } } } return newObj; } var object = [ { title: 'test', checked: false } ]; deepCopy(object);
const StartScore = rate => "★★★★★☆☆☆☆☆".slice(5 - rate, 10 - rate); const start = StartScore(3); // start => "★★★"
const arr = [1, 2, 3, [4, 5, [6, 7]]]; const flatten = arr.toString().split(','); console.log(flatten);
优势:简单,方便,对原数据没有影响
缺点:最好数组元素全是数字或字符,不会跳过空位web
const arr = [1, 2, 3, [4, 5, [6, 7]]]; const flatten = arr.join(',').split(','); console.log(flatten);
优势和缺点同toString数组
const arr = [1, 2, 3, [4, 5, [6, 7]]]; const flatten = arr.flat(Infinity); console.log(flatten);
优势:会跳过空位,返回新数组,不会修改原数组app
const arr = [1, 2, 3, [4, 5]]; console.log([].concat(...arr));
优势:简单,方便
缺点:只能扁平化一层框架
// 不使用:not() .nav li { border-right: 1px solid #666; } .nav li:last-child { border-right: none; } // 使用:not() .nav li:not(:last-child) { border-right: 1px solid #666; } // 或者使用兄弟选择符~ .nav li:first-child ~ li { border-left: 1px solid #666; }
移动设备相对来讲页面较小,不少时候显示的一些信息都须要省略部分。最多见的是单行标题溢出省略,多行详情介绍溢出省略。如今都用框架开发了,这种建议需求建议造成一个基础组件,方便快捷函数
//单行 .single { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; } //多行 .more { display: -webkit-box !important; overflow: hidden; text-overflow: ellipsis; work-break: break-all; -webkit-box-orient: vertical; -webkit-line-clamp: 2; //指定行数 }
伴随整个项目周期的分支测试
从master切,顾名思义,开发每个功能的分支,开发完的功能合并到release分支。this
从master切,修复BUG分支,测试完直接合并到master。spa
从master切,须要测试的功能都合并到该分支上进行测试。prototype
一旦开发完成,就会把release分支合并到master分支,并删除原分支。
常用列表,好比待办事项列表、购物车等,若是数据不太多的话,列表就显得尤其有用
function list() { this.dataStore = []; //初始化数组 this.clear = clear; //清除列表 this.remove = remove; //移除列表中的元素 this.find = find; //寻找列表中的元素 this.length = length; //返回列表的长度 } function find(element) { for (var i = 0, len = this.dataStore.length; i < len; i++) { if (this.dataStore[i] === element) { return i; } } return -1; } function remove(element) { for (var i = 0, len = this.dataStore.length; i < len; i++) { if (this.dataStore[i] === element) { this.dataStore.splice(i, 1); } } return this.dataStore; } function length() { return this.dataStore.length; } function clear() { this.dataStore = []; }