我正在使用 webpack 和 ES6 类。我试图在 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/