// 注入式多文件选择功能 - 仅保留triggerAllFileSelect相关逻辑
(function() {
// 避免重复注入
if (document.getElementById('inject-file-selector')) return;
// 1. 创建并注入DOM元素
function createDOM() {
// 容器
const container = document.createElement('div');
container.id = 'inject-file-selector';
container.style.cssText = `
position: fixed;
top: 20px;
right: 20px;
z-index: 99999;
background: #fff;
padding: 15px;
border: 1px solid #ccc;
border-radius: 8px;
min-width: 300px;
`;
// 标题
const title = document.createElement('h4');
title.textContent = '多文件选择测试';
title.style.margin = '0 0 10px 0';
title.style.fontSize = '16px';
// 选择按钮
const btn = document.createElement('button');
btn.id = 'inject-select-btn';
btn.textContent = '选择任意文件(多文件)';
btn.style.padding = '8px 16px';
btn.style.background = '#007bff';
btn.style.color = '#fff';
btn.style.border = 'none';
btn.style.borderRadius = '4px';
btn.style.cursor = 'pointer';
btn.style.marginBottom = '10px';
// 隐藏的文件输入框
const fileInput = document.createElement('input');
fileInput.id = 'inject-file-input';
fileInput.type = 'file';
fileInput.multiple = true;
fileInput.style.display = 'none';
// 信息展示区域
const infoArea = document.createElement('div');
infoArea.id = 'inject-file-info';
infoArea.style.fontSize = '12px';
infoArea.style.maxHeight = '200px';
infoArea.style.overflowY = 'auto';
infoArea.style.marginTop = '10px';
infoArea.innerHTML = '未选择任何文件';
// 组装DOM
container.appendChild(title);
container.appendChild(btn);
container.appendChild(fileInput);
container.appendChild(infoArea);
document.body.appendChild(container);
return {
btn: btn,
fileInput: fileInput,
infoArea: infoArea
};
}
// 2. 核心函数:触发多文件选择
function triggerAllFileSelect() {
const dom = createDOM();
// 按钮点击触发文件选择
dom.btn.addEventListener('click', function() {
dom.fileInput.click();
log('用户点击选择文件按钮', dom.infoArea);
});
// 处理文件选择结果
dom.fileInput.addEventListener('change', function() {
const files = Array.from(this.files);
if (files.length === 0) {
log('用户取消了文件选择', dom.infoArea);
dom.infoArea.innerHTML = '未选择任何文件';
return;
}
// 展示文件信息
log(`成功选择 ${files.length} 个文件`, dom.infoArea);
let infoHtml = `<strong>共选择 ${files.length} 个文件:</strong><br><br>`;
files.forEach((file, index) => {
const size = (file.size / 1024).toFixed(2) + 'KB';
infoHtml += `文件${index+1}:<br>
• 名称:${file.name}<br>
• 大小:${size}<br>
• 类型:${file.type || '未知'}<br><br>`;
});
dom.infoArea.innerHTML = infoHtml;
// 清空input值,支持重复选择同一文件
this.value = '';
});
}
// 辅助函数:日志记录
function log(message, infoArea) {
const time = new Date().toLocaleTimeString();
console.log(`[${time}] ${message}`); // 控制台日志
// 可选:在页面信息区域也显示日志
// infoArea.innerHTML += `<br>[${time}] ${message}`;
}
// 3. 执行注入
triggerAllFileSelect();
// 暴露全局函数(可选,方便手动调用)
window.triggerAllFileSelect = triggerAllFileSelect;
})();
H5页面,注入式多文件选择功能
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- 问题描述:App中嵌入h5页面,调起app native之后返回h5页面 此时刷新页面(如支付点击已完成未完成)注...
- 1页面链接检查每一个链接是否都有对应的页面,并且页面之间切换正确; 2相关性检查删除/增加一项会不会对其他项产生影...