- 2017.10.26 属性名和属性值获取
获取js对象的所有属性的属性名数组,如获取obj的属性名数组:
var propArr = Object.getOwnPropertyNames(obj).sort();
另一种获取属性名:1
2
3for(attribute in obj){
alert(attribute);
}
获取属性名对应属性值:1
2
3for(attribute in obj){
alert(obj[attribute]);
}
2017.11.21 Snippets
- 小数保留*位小数,整数保持整数
1
2
3
4//如果原来是整数等就取原来的整数,原始值为小数保留两位小数
if (value.toString().split(".").length > 1) {
value= value.toFixed(2)
}
- 小数保留*位小数,整数保持整数
时间戳格式化
1
2
3
4
5
6
7
8
9
10
11
12
13
14//将时间戳格式化为yyyy-MM-dd hh:mm:ss形式
function formatDate(timestamp) {
var date = new Date(timestamp);
Y = date.getFullYear() + '-';
M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
D = date.getDate() + ' ';
h = date.getHours() + ':';
// m = (date.getMinutes()<10?'0'+date.getMinutes():date.getMinutes()) + ':';
m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
// var result = Y + M + D + h + m + s;
var result = Y + M + D + h + m;
return result;
}generate sku table
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var result = [];//组合成产品规格集
/* specResultList
0:{id: 36, attrName: "容量", alias: null, classifyId: 11, propValues: Array(6), …}
1:{id: 43, attrName: "颜色", alias: null, classifyId: 11, propValues: Array(7), …}
2:{id: 69, attrName: "尺寸", alias: null, classifyId: 11, propValues: Array(12), …}
propValues
0:{id: 117, propValue: "锥形1500", propName: null, propNameId: 36, gmtDatetime: null, …}
1:{id: 118, propValue: "锥形1800", propName: null, propNameId: 36, gmtDatetime: null, …}
2:{id: 119, propValue: "锥形1000", propName: null, propNameId: 36, gmtDatetime: null, …}
3:{id: 231, propValue: "250ML", propName: null, propNameId: 36, gmtDatetime: "2017-09-19 18:59:08", …}
4:{id: 232, propValue: "350ML", propName: null, propNameId: 36, gmtDatetime: "2017-09-19 18:59:08", …}
5:{id: 233, propValue: "450ML", propName: null, propNameId: 36, gmtDatetime: "2017-09-19 18:59:08", …}
*/
//某分类下规格结果集合
var specResultList = [];
//根据规格集合生成sku笛卡尔积
function generate(index, current) {
if (index < specResultList.length - 1) {
var specItem = specResultList[index];
var keya = specItem.attrName;
var items = specItem.propValues;
if (items.length == 0) {
generate(index + 1, current);
}
for (var i = 0; i < items.length; i++) {
if (!items[i]) continue;
var newMap = {};
newMap = $.extend(newMap, current);
newMap[keya] = items[i];
generate(index + 1, newMap);
}
} else if (index == specResultList.length - 1) {
var specItem = specResultList[index];
var keya = specItem.attrName;
var items = specItem.propValues;
if (items.length == 0) {
result.push(current);
}
for (var i = 0; i < items.length; i++) {
if (!items[i]) continue;
var newMap = {};
newMap = $.extend(newMap, current);
newMap[keya] = items[i];
result.push(newMap);
}
}
}
1 | //生成sku表格,参数result为转化后的sku数组 |
- sku表格创建
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22//表格创建
$(document).on("change", '.sku_value', function () {
var skuTypeArr = [];//存放SKU类型的数组
var totalRow = 1;//总行数
//获取所选规格数组
skuTypeArr = getCkeckedSkuTypeArr(skuTypeArr, totalRow);
/* skuTypeArr结果形如
0:{attrName: "试试", propNameId: "26", propValues: Array(1), skuValueLen: 1}
1:{attrName: "你好", propNameId: "24", propValues: Array(1), skuValueLen: 1}*/
if (skuTypeArr.length == 0) {
$(".sku-table").html("");
} else {
//已选规格集合
specResultList = skuTypeArr;
result = [];
//做规格笛卡尔积,转化规格为多个sku
generate(0, {});
//生成sku表格
generateSkuTable(result);
}
});
- 2017.11.27 获取页面宽度,设置字体大小
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//获取页面宽度,设置字体大小
(function (doc, win) {
var docEl = doc.documentElement,
resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
recalc = function () {
var clientWidth = docEl.clientWidth;
if (!clientWidth) return;
if (clientWidth < 750) {
docEl.style.fontSize = 100 * (clientWidth / 750) + 'px';
}
else {
docEl.style.fontSize = '100px';
}
};
if (!doc.addEventListener) return;
win.addEventListener(resizeEvt, recalc, false);
doc.addEventListener('DOMContentLoaded', recalc, false);
})(document, window);
1 | //setTimeout轮询 |
- 2019.01.23 Snippet
1
2
3
4
5//限制input位数和数字
oninput="if(value.length>6)value=value.slice(0,6);value=value.replace(/[^\d]/g,'')"
//小数
oninput="if(value.length>6)value=value.slice(0,6);value=value.replace(/[^\d\.]/g,'')"
1 | function timestampToTime(timestamp) { |
1 | objPropertyRecursively2String(obj) { |
1 | if (openInWebview()) { |
- Service Worker
1 | Service workers也可以用来做这些事情: |
npm 安装包时 npm ERR! code Z_BUF_ERROR
2019.02.05 安装hexo的git部署模块 hexo-deploy-git 的时候屡次报错
npm ERR! code Z_BUF_ERROR
由google后在爆栈网中找到错误原因为npm缓存的问题,所以照着answer清除npm缓存
npm cache clean --force
npm cache verify
重试之后可行。但是感觉隐隐约约不是这个问题,就看了看其他中文回答,说是访问外网网络不稳定原因(我想了下是有可能的,因为中间切换了一下自己SSR的配置),用配置阿里npm代理的cnpmnpm install -g cnpm --registry=https://registry.npm.taobao.org
。几个方法都列举了下,谨作参考。