Skip to content

Instantly share code, notes, and snippets.

@jerrylususu
Created August 19, 2024 15:35
Show Gist options
  • Save jerrylususu/07b44fb6b8c9cd298bb9eb69d26820fa to your computer and use it in GitHub Desktop.
Save jerrylususu/07b44fb6b8c9cd298bb9eb69d26820fa to your computer and use it in GitHub Desktop.
hack element-ui DateTimePicker
javascript:(function() {
// 获取当前时间
var now = new Date();
var formattedTime = now.toISOString().slice(0, 16); // 格式化为 YYYY-MM-DDTHH:MM
// 找到 DateTimePicker 的 input 元素
var inputElement = document.querySelector('.el-date-editor input');
if (inputElement) {
// 模拟用户点击 input 元素
inputElement.click();
// 设置 input 的 value 属性
inputElement.value = formattedTime;
// 创建并分发 input 事件
var inputEvent = new Event('input', { bubbles: true });
inputElement.dispatchEvent(inputEvent);
// 创建并分发 change 事件
var changeEvent = new Event('change', { bubbles: true });
inputElement.dispatchEvent(changeEvent);
// 模拟用户失去焦点
inputElement.blur();
} else {
console.error('DateTimePicker input element not found');
}
})();
@jerrylususu
Copy link
Author

may also need focus event

@jerrylususu
Copy link
Author

input.dispatchEvent(new Event("focus", {bubbles: true})
input.dispatchEvent(new KeyboardEvent("keydown", {key: 'Esc', keyCode: 27, bubbles: true})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment