一、复制文字到剪切板
// 复制到剪切板
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 "";
}