javascript - 无法使用带有 webpack 和 ES6 设置的 document.getElementById ('password').addEventListener() 调用函数

标签 javascript webpack ecmascript-6 addeventlistener es6-class

我正在使用 webpackES6 类。我试图在 input#password 上调用 'onblur' 函数,但它不起作用。它既不抛出任何错误也不调用回调函数。如果我尝试使用 console.log(document.getElementById('password')) 在 eventListener 之外找到 input#password,那么它会给我 input#password 作为 DOM 对象。我不知道我哪里出了问题。下面是我的代码:

index.html

    <head>
      <script src="./bundle.js"></script>
    </head>
    <body>
       <input id="password" type="password" placeholder="Please enter your password" autofocus="autofocus">
    </body>

webpack-config.js

const path = require('path');

module.exports = {
    entry: './src/index.js',
    output: {
        filename: 'bundle.js',
    },
    devServer: {
        contentBase: path.join(__dirname, 'dist')
    }
};

索引.js

import { Password } from './app/main';

document.getElementById('password').addEventListener('onblur', () => {
    console.log('im here');
    var initPwd = new Password();
    initPwd.init();
})

主要.js

class Password {
    init() {
        console.log('I am inside init.')
    }
}

export { Password }

请帮忙!

最佳答案

document.getElementById('password').addEventListener('onblur', () => {

=>

document.getElementById('password').addEventListener('blur', () => {

onblur 可与 element.onblur = function() {} 一起使用,因此当您使用 addEventListener 接口(interface)时,您不应编写 on 在事件名称中。

关于javascript - 无法使用带有 webpack 和 ES6 设置的 document.getElementById ('password').addEventListener() 调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52800042/

相关文章:

javascript - 我什么时候应该在进口中使用括号

javascript - 处理对象数组更新响应的多种方式

javascript - 更新到RC5后注入(inject)错误

visual-studio - 带有 Visual Studio 2013 和 TypeScript 的 SystemJS 模块

javascript - php中如何自动更改时间?

php - 使用 Silex 框架在 Twig 模板中使用 Javascript 变量

javascript - 页面 anchor 链接的 jQuery this.hash 行为

Webpack 在最终包中包含未使用的导出(不是摇树)

javascript - 在 webpack 4 项目中使用 Jest 会抛出 'Jest encountered an unexpected token' 错误

javascript - 将 JavaScript 转换为 VBScript