31 lines
1.2 KiB
JavaScript
31 lines
1.2 KiB
JavaScript
const vTrim = {
|
|
install(Vue, options) {
|
|
Vue.directive('fac', {
|
|
componentUpdated: (el, {value, modifiers}) => {
|
|
let input = el
|
|
input.oninput = (event) => {
|
|
console.log(event.target.value)
|
|
// event.target.value = event.target.value.replace(/(^\s*)|(\s*$)/g, "")
|
|
input.value = input.value.replace(/(^\s*)|(\s*$)/g, '')
|
|
}
|
|
// // 获取当前光标所在位置
|
|
// const inputSelection = input.selectionStart
|
|
// // 获取加上空格后的字符串长度
|
|
// const valueLength = input.value.length
|
|
// // 这里是去除空格,可以通过其他的正则去做其他的操作(格式化手机号啊身份证号码之类的,后续也可以通过指令传入拓展成各种格式化指令)
|
|
// let newValue = value.replace(/\s/g, '')
|
|
// if (value !== newValue) {
|
|
// input.value = newValue
|
|
// if (input.value.length !== valueLength) {
|
|
// input.selectionStart = inputSelection - 1
|
|
// input.selectionEnd = inputSelection - 1
|
|
// }
|
|
// input.dispatchEvent(new Event(modifiers.lazy ? 'change' : 'input'))
|
|
// }
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
export default vTrim
|