请问addEventListener绑定change事件触发后event.target为null呢?
const input = document.createElement('input')
input.type = 'file'
input.accept = '.doc, .docx'
input.addEventListener('change', handleFileSelect, false)
function handleFileSelect (event) {
    console.log(111, event.target) // 这里为什么会为 null 呢?
}回复
1个回答

test
2024-06-21
    const input = document.createElement('input')
    input.type = 'file'
    input.accept = '.doc, .docx'
    input.addEventListener('change', handleFileSelect, false)
    function handleFileSelect(event) {
      console.log(111, event.target) // 这里为什么会为 null 呢?
    }
// 把input标签追加为body子元素
    document.body.appendChild(input);经过测试,当change事件触发的时候并不是null,而是input标签。
回复

适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容
