这个时间选择组件在输入的时候是监听不到输入的值的,所以我们在外层再套个div,然后用获取焦点事件去操作dom
页面中
<div id="inParkingData">
<el-date-picker
v-model="indateRange"
size="small"
value-format="yyyy-MM-dd HH:mm:ss"
type="datetimerange"
range-separator="-"
start-placeholder="开始日期"
end-placeholder="结束日期"
:default-time="['00:00:00', '23:59:59']"
@focus="inParkingDataFocus('inParkingData', indateRange)"
>
</el-date-picker>
</div>
js文件中
// 获取 dom 方法
export function gainFocus(e, data) {
console.log(e, data, "==11==");
var _self = this;
var inDataInput = document.getElementById(e).getElementsByTagName("input")[0];
var outDataInput = document
.getElementById(e)
.getElementsByTagName("input")[1];
inDataInput.addEventListener("input", inTapEvent);
outDataInput.addEventListener("input", outTapEvent);
function inTapEvent() {
console.log(inDataInput.value);
setTimeout(() => {
inDataInput.value = disposeData(inDataInput.value);
}, 100);
Vue.set(data, 0, inDataInput.value);
}
function outTapEvent() {
setTimeout(() => {
outDataInput.value = disposeData(outDataInput.value);
}, 100);
Vue.set(data, 1, outDataInput.value);
}
return data;
}
// 更改格式
function disposeData(str) {
var value = str.split("");
if (value.length == 4) {
value.splice(4, 0, "-");
}
if (value.length == 7) {
value.splice(7, 0, "-");
}
if (value.length == 10) {
value.splice(10, 0, " ");
}
if (value.length == 13) {
value.splice(13, 0, ":");
}
if (value.length == 16) {
value.splice(16, 0, ":");
}
return value.join("");
}