web前端常用的公共方法

一、复制文字到剪切板

  // 复制到剪切板
copyToClipboard(str) {
    const el = document.createElement('textarea');
    el.value = str;
    el.setAttribute('readonly', '');
    el.style.position = 'absolute';
    el.style.left = '-9999px';
    document.body.appendChild(el);
    el.select();
    document.execCommand('copy');
    document.body.removeChild(el);
  },

二、去两端空格,中间的空格或者逗号转化成一个空格,再分割成数组

// 切割成数组, 常应用于文本输入框 空格搜索多个
const str = '  RDBH-202105300755      002.001.0006580,wqe,RDBH-202105300755	002.001.000	RDBH-202105300755	002.001.00065801	RDBH-2    '
const arr = str.split(/[,,\s\n]/).filter(_ => _)
const arr2 = str.replace(/[,,\s]+/gmi, ',').split(',').filter(_ => _)
console.log(arr,arr11) // 都是length为8的数组

三、根据汉字拼音 > 字母 > 数字 > 空白字符串的简单排序。

  //根据汉字拼音 > 字母 > 数字 > 空白字符串的简单排序 zejun-订单中心
  newSortByNameStr(a, b, fieldName) {
    const strA = a[fieldName];
    const strB = b[fieldName];
    if (strA && !strB) {
      return -1
    }
    if (!strA && strB) {
      return 1
    }
    if (!strA && !strB) {
      return 0
    }
    if(typeof strA === 'number' && typeof strB  === 'number' && strA <= strB){
      return 1
    }else if( typeof strA === 'number' && typeof strB  === 'number' && strA > strB){
      return -1
    }
    if (strA && strB) {
      if ((strA.charCodeAt(0) > 256) && (strB.charCodeAt(0) > 256)) {
        return strB.localeCompare(strA, 'zh')
      } else if ((strA.charCodeAt(0) > 256) && (strB.charCodeAt(0) < 256)) {
        return -1
      } else if ((strA.charCodeAt(0) < 256) && !strB) {
        return -1
      } else {
        return strB.localeCompare(strA)
      }
    }
  },
// 运用 数组的排序 返回即可
data.sort(a,b) =>{ return newSortByNameStr(a,b,item.dataIndex) };

四、去除对象中为空的属性

// 去除对象中为空的属性
function handleValue(data) {
    for ( var key in data ){
        if ( data[key] === '' || data[key] === undefined ){
            delete data[key]
        }
    }
    return data;
}

五、判断浏览器类型

// 判断浏览器类型
function myBrowser() {
const userAgent = navigator.userAgent; // 取得浏览器的userAgent字符串
const isOpera = userAgent.indexOf("Opera") > -1;
if (isOpera) {
  return "Opera";
}
if (userAgent.indexOf("Firefox") > -1) {
  return "FF";
}
if (userAgent.indexOf("Chrome") > -1) {
  return "Chrome";
}
if (userAgent.indexOf("Safari") > -1) {
  return "Safari";
}
if (
  userAgent.indexOf("compatible") > -1 &&
  userAgent.indexOf("MSIE") > -1 &&
  !isOpera
) {
  return "IE";
}
if (userAgent.indexOf("Trident") > -1) {
  return "Edge";
}
return "";
}

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值