1:时间戳转换成年月日函数,2:url截取参数方法,3:弹窗自定义方法 4:点击按钮加入购物车
最近一直在使用vue.js来构建项目,先分享一下一些简单可复用的函数。 1:时间戳转换
Date.prototype.format = function(fmt){ //author: yumeiqiang
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"w+":'星期'+this.getDay(),
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
};
使用方法为 new Date(时间戳).format("yyyy-MM-dd w hh:mm:ss")可随意在format函数中自定义添加 2:URL参数截取函数
var getUrl = {
getUrlParam: function (name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)
||[,""])[1].replace(/\+/g, '%20'))||null;
}
};
使用方法:假设URL链接为http://www.baidu.com?from=yu&id=13456
截取from的参数为 getUrl.getUrlParam('from')可得到yu
3:自定义弹窗
var alertFn = function (text) {//1秒倒计时
document.getElementById('alert1').style.display='block';
var time = 1;
document.getElementById('alert1').innerHTML=text;
Showalert(time);
};
var Showalert = function (time) {
setTimeout(function () {
time--;
if (time < 0) {
document.getElementById('alert1').style.display='none'
} else {
Showalert(time);
}
}, 1000);
};
使用方法:在html中标签例如 <div class='alert1' id='alert1'></div> id名称与上述中保持一致,然后自定义css弹窗样式与内容,
然后只需在需要使用弹窗的地方调用 alertFn()即可
4:点击按钮加入购物车
var cart = function($event){
var el=$event.currentTarget;
var tkId=el.getAttribute("data-id");
var name=el.getAttribute("data-name");
var price=el.getAttribute("data-price");
var img=el.getAttribute("data-img");
var reserved=el.getAttribute("data-order");
this.message++;
var product = {
'tkId': tkId,
'tkName': name,
'num':1,
'img':img,
'price': parseFloat(price),
'reserved': parseInt(reserved),
'pid': getUrl.getUrlParam('share') ? getUrl.getUrlParam('share') : 0
};
console.log(product.pid);
//添加购物车
var ShoppingCart = localStorage.getItem('ShoppingCart');
if(ShoppingCart==null||ShoppingCart==""){
//第一次加入商品
var jsonStr = {"productlist":[{"img":product.img,"tkId":product.tkId,"tkName":product.tkName,"num":product.num, "price":product.price, "reserved":product.reserved, "pid": product.pid}],
"totalNumber":product.num,"totalAmount":(product.price * product.num)};
localStorage.setItem("ShoppingCart", JSON.stringify(jsonStr));
console.log(JSON.stringify(jsonStr));
} else {
// add goods
var jsonStr = JSON.parse(ShoppingCart);
var productlist = jsonStr.productlist;
var result=false;
//查找购物车中是否有该商品
for(var i in productlist) {
if(productlist[i].tkId == product.tkId){
productlist[i].num++;
productlist[i].pid = product.pid;
jsonStr.totalNumber++;
jsonStr.totalAmount += parseFloat(product.price);
result = true;
}
}
if(!result){
//没有该商品就直接加进去
productlist.push({"img":product.img, "tkId":product.tkId, "tkName":product.tkName, "num":product.num, "price":product.price, "reserved":product.reserved, "pid": product.pid});
jsonStr.totalNumber++;
jsonStr.totalAmount += parseFloat(product.price);
}
//保存购物车
localStorage.setItem("ShoppingCart", JSON.stringify(jsonStr));
}
}
1:时间戳转换成年月日函数,2:url截取参数方法,3:弹窗自定义方法 4:点击按钮加入购物车的更多相关文章
- js angular 时间戳转换成日期格式 年月日 yyyy-MM-dd
昨天写项目,要把时间戳转换成日期格式发给后端 我就去网上找 看到的一些都不是我想要的 索性自己就写了一个如图 下面是angular 模式 $scope.getMyDate = function(str ...
- vue element-ui表格里时间戳转换成时间显示
工作中遇到后台给的表格数据里时间是一个13位的时间戳,需要转换成时间显示在表格里, 可以用element-ui表格自带的:formatter函数,来格式化表格内容: // 时间戳转换成时间 // 使用 ...
- 将日期转换为指定的格式:比如转换成 年月日时分秒 这种格式:yyyy-MM-dd hh:mm:ss 或者 yyyy-MM-dd。总结下。
可以为Date原型添加如下的方法: Date.prototype.format = function(fmt) { var o = { "M+" : this.getMonth() ...
- 时间戳转换成日期的js
在项目开发过程中,我们常常需要把时间戳转换成日期.下面这个是我一直使用的js方法,希望能帮助到有需要的朋友.大家如果有更好的方法,请多多指教! js代码如下: //时间戳转换成日期 function ...
- unix时间戳转换成标准时间(c#)
//---unix时间戳转换成标准时间(c#)---// /* string timeStamp = "1144821796"; DateTime dtSt ...
- C#将unix时间戳转换成.net的DateTime类型的代码
下面的内容是关于C#将unix时间戳转换成.net的DateTime类型的内容. DateTime epoch = new DateTime(1970,1,1,0,0,0,0, DateTimeKin ...
- js中时间戳转换成时间格式
js中时间戳转换成时间格式, // 时间戳转换成时间格式 var formatDate = function(date){ date = new Date(date); var y=date.getF ...
- Java-Runoob-高级教程-实例-时间处理:04. Java 实例 - 时间戳转换成时间
ylbtech-Java-Runoob-高级教程-实例-时间处理:04. Java 实例 - 时间戳转换成时间 1.返回顶部 1. Java 实例 - 时间戳转换成时间 Java 实例 以下实例演示 ...
- C# 13位时间戳转换成标准时间C#代码
原地址:https://www.cnblogs.com/yixuehan/p/5559244.html /// <summary> /// 时间戳转换成标准时间 /// </summ ...
随机推荐
- spring bean生命周期管理--转
Life Cycle Management of a Spring Bean 原文地址:http://javabeat.net/life-cycle-management-of-a-spring-be ...
- Android基于mAppWidget实现手绘地图(十一)–移动地图到某个坐标
你可以使用以下几个方法: MapWidget.scrollMapTo(android.location.Location location); MapWidget.scrollMapTo(androi ...
- .NET知识结构
.NET知识结构 .NET介绍 微软.NET战略及技术体系,.NET Framework框架类库(FCL),公共语言运行时(CLR),通用类型系统(CTS),公共语言规范(CLS),程序集(Assem ...
- 在当前Server上找某某object,注意只需修改"要找的object"就可以使用
---在当前Server上找某某object,注意只需修改"要找的object"就可以使用EXEC sp_MSforeachdb 'use ? ;IF EXISTS(SELECT ...
- 轻松实现localStorage本地存储
相信大家都知道HTML5提供了localStorage和sessionStorage两个新功能,基于这两个功能我们可以实现web资源的离线和会话存储,如果你现在还在用Cookie来临时存储网络资源的话 ...
- 使用Python进行GUI操作自动化
前言 本文介绍怎样使用Python进行跨平台的GUI操作的,其中使用的一个工具包是pyautogui,PyAutoGUI可以模拟鼠标的移动.点击.拖拽,键盘按键输入.按住操作,以及鼠标+键盘的热键同时 ...
- 【转】ASP.NET"正在中止线程"错误原因
最近做的系统中老出现的一些问题不太明白,在使用 Response.End.Response.Redirect 或 Server.Transfer 时出现 ThreadAbortException , ...
- DES加密解密
加密后生成Base64字符串,并去除'='字符. 加密后替换掉'+',这样加密后的字符串可以作为url参数传递. using System; using System.IO; using System ...
- Android ADB 命令大全
1 cpu号: 文件在: /proc/cpuinfo 通过Adb shell 查看: adb shell cat /proc/cpuinfo 2 mac 地址 文件路径 /sys/class/net/ ...
- “Win10 UAP 开发系列”之 在MVVM模式中控制ListView滚动位置
这个扩展属性从WP8.1就开始用了,主要是为了解决MVVM模式中无法直接控制ListView滚动位置的问题.比如在VM中刷新了数据,需要将View中的ListView滚动到顶部,ListView只有一 ...