JS方法记录

424 阅读1分钟

一键复制

copyMould(text){
  let copyTextarea = document.createElement("textarea")
  copyTextarea.value = text
  document.body.appendChild(copyTextarea)
  copyTextarea.select()
  document.execCommand("Copy")
  document.body.removeChild(copyTextarea)
  this.$message({
    message: '复制此模板成功',
    type: 'success'
  })
}

获取地址栏参数

export function getUrlKey (name) {
  /* eslint-disable */
  return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1].replace(/\+/g, '%20')) || null
  /* eslint-disable */
}

首字母大写

export function firstUpperCase (str) {
  return str.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase())
}

复制对象重新赋值不改变原对象

export function cloneObjectFn (obj) {
  return JSON.parse(JSON.stringify(obj))
}

解决input number类型上下滚动,禁用滚轮事件

export function stopInputScrollFun (e) {
  e = e || window.event
  if (e.preventDefault) {
    e.preventDefault()
    e.stopPropagation()
  } else {
    e.cancelBubble = true
    e.returnValue = false
  }
  return false
}

// 使用
<el-input 
  type="number"
  @mousewheel.native.prevent="stopInputScrollFun"
  v-model.trim="num"></el-input>

中国标准时间转换为yyyy-MM-dd

export function transformationDate (date) {
  return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate() > 9 ? date.getDate() : `0${date.getDate()}`}`
}