当前位置:网站首页>js方法封装汇总
js方法封装汇总
2022-07-20 05:33:00 【知了还没睡】
//根据文件路径获取扩展名
getKzName(str){//获取文件路径的扩展名
var url = str
var pos = url.lastIndexOf(".");
if(pos == -1){
pos = url.lastIndexOf("\\")
}
ar filename = url.substr(pos +1);
eturn filename
},
getKzName("xxxxx.mp4") //mp4
//判断是否手机号码
export function isPhone(str) {
const reg = /^[1][3,4,5,7,8,9][0-9]{9}$/
if (!reg.test(str)) {
return false;
} else {
return true;
}
}
//根据key截取url传参
export const getQueryString = (name) => {
let urlArr = window.location.href.split('?');
let str = null
if (urlArr.length == 1) {
str = null
return str
} else {
urlArr = urlArr[1]
urlArr = urlArr.split('&')
urlArr.forEach(v => {
if (v.indexOf(name) != -1) {
str = v.split('=')[1] != undefined ? v.split('=')[1] : null
}
})
return str;
}
}
//将url上的参数转为json对象
export const getUrlParams=(str="?")=>{
let urlstr = location.href;
console.log(str.split(str)[1])
let obj = {};
if(urlstr.split(str)[1]){
let arr = urlstr.split(str)[1].split("&"); //先通过?分解得到?后面的所需字符串,再将其通过&分解开存放在数组里
for (let i of arr) {
obj[i.split("=")[0]] = i.split("=")[1]; //对数组每项用=分解开,=前为对象属性名,=后为属性值
}
console.log("obj",obj);
}else{
console.log("无");
}
return obj
}
//时间格式化
export const formatDate = (date, fmt) => {
date = date || new Date();
fmt = fmt || 'yyyy-MM-dd hh:mm:ss';
if (Object.prototype.toString.call(date).slice(8, -1) !== 'Date') {
date = new Date(date)
}
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
}
let o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds()
}
for (let k in o) {
if (new RegExp(`(${k})`).test(fmt)) {
let str = o[k] + ''
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : (("00" + o[k]).substr(("" + o[k]).length)))
}
}
return fmt
}
formatDate("时间戳",'YYYY-MM-dd HH:dd:ss')
//防抖
export const antiShake= (fn, t) => {
let delay = t || 1000
let timer
return function () {
let args = arguments;
if (timer) {
clearTimeout(timer)
}
let callNow = !timer
timer = setTimeout(() => {
timer = null
}, delay)
if (callNow) fn.apply(this, args)
}
}
eventName:antiShake(function(){
//do thing
})
自定义全局指令
//main.js中
//自定义指令,价钱保存两位小数(每三位加逗号)
封装法一:
Vue.filter('moneyTwo', function(val) {
val = Number(val)
if(isNaN(val)) {
val = 0;
}
return Number(val.toFixed(2)).toLocaleString('zh-CN', { minimumFractionDigits: 2 })
})
封装法二:
Vue.filter('moneyTwo', function(val) {
val = Number(val)
val = val.toString().replace(/\$|\,/g,'');
if(isNaN(val)) {
val = "0";
}
let sign = (val == (val = Math.abs(val)));
val = Math.floor(val*100+0.50000000001);
let cents = val%100;
val = Math.floor(val/100).toString();
if(cents<10) {
cents = "0" + cents
}
for (var i = 0; i < Math.floor((val.length-(1+i))/3); i++) {
val = val.substring(0,val.length-(4*i+3))+',' + val.substring(val.length-(4*i+3));
}
return (((sign)?'':'-') + val + '.' + cents);
})
//使用
{
{Numvalue | moneyTwo}}
js文档:
边栏推荐
- Application practice of shengteng industrial quality inspection
- (9) Pytorch deep learning: convolutional neural network (the inception module in googlenet network architecture is this convolutional neural network architecture)
- Chapter006-FPGA学习之LCD显示
- Clickhouse failed to start_ Unit clickhouse-server. service entered failed state
- Xiaobai tutorial -- Anaconda's jupyter notebook automatic completion configuration tutorial
- Data generator - supports multiple types
- 直观理解Dilated Convolution
- SQL subquery
- 建筑工程测量与测绘毕业论文范文
- PyQt5 使用自定义ToolTip解决QTableWidget数据显示不全问题
猜你喜欢
(4) Pytorch deep learning: pyttorch realizes linear regression
理解WGAN 和 Spectral Normalization(归一化)
Chapter004-FPGA学习之IP核相关功能【时钟、RAM、FIFO】
Neural networks: a review of 2D target detection
qlineargradient中坐标的含义
(5) Pytorch deep learning: logistic regression
pytorch实现手写数字识别 | MNIST数据集(全连接神经网络)
【CANN训练营】CANN训练营_昇腾AI趣味应用实现AI趣味应用(上)随笔
建筑装饰毕业论文题目
[camp d'entraînement can] essai de mise en œuvre du Gan basé sur Shengsi
随机推荐
Flink datastream API (I) execution environment
Play with the one-stop plan of cann target detection and recognition [introduction]
(5) Pyqt5 series tutorials: use pychart to design the internal logic of pyqt5 in the serial port assistant parameter options (II)
Package (.Py) files into (.Exe) files in pychart
There is a lot of garbage on wechat on the computer side that can be cleaned up
Intuitively understand the transfer revolution
#HPDC 华为伙伴暨开发者大会2022随笔
基于STM32CubeMX的片外SPIFLASH嵌入FATFS
Docker remote connection MySQL
(2) Pytorch deep learning: gradient descent
Tensorflow 1.x 和 Pytorch 中 Conv2d Padding的区别
[cann training camp] cann training camp_ Shengteng AI interesting application realizes AI interesting application (Part 2) essay
针对嵌入式设备外接设备的驱动开发心得
【CANN训练营】基于昇思的GAN实现随笔
从购买服务器到搭建个人博客网站 | 图文详细过程(腾讯云|宝塔面板|wordpress|Argon)
辨析Iteration、epoch及batchsize之间的关系
电力系统及其自动化毕业论文题目【精选】
Tensorflow V1 introductory tutorial
MIMO-OFDM無線通信技術及MATLAB實現(2)-SISO下的室外信道模型
HPDC Huawei partners and Developers Conference 2022 essay