无法读取null的属性'addEventListener',但jsfiddle工作

时间:2017-06-29 02:32:12

标签: javascript html5

这是我第一次遇到这样的情况......我正在Apache环境中运行xampp服务器,我正在登录页面,我想在点击时放置一个显示密码的JavaScript按钮。它不适用于我的环境,但它正在jsfiddle上运行。我提供了以下代码:

document.getElementById("eyeButton").addEventListener("click", function (e) {
    'use strict';
    var pwd = document.getElementById("password");
    if (pwd.getAttribute("type") === "password") {
        pwd.setAttribute("type", "text");
    } else {
        pwd.setAttribute("type", "password");
    }
});
<input 
       type="password" 
       id="password" 
       name="password" 
       maxlength="50" 
       placeholder="Enter password..." 
       required 
       pattern="(?=^.{8,}$)(?=.*\d)(?=.*\W+)(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$"
       title="Password must contain at least eight characters and one uppercase letter, one lowercase letter, one number, and one special character.">
        <button type="button" id="eyeButton">
            <img src="https://cdn0.iconfinder.com/data/icons/feather/96/eye-16.png" alt="eye">
        </button>

JavaScript代码已正确链接到页面,因此我不知道为什么会出现Cannot read property 'addEventListener' of null错误。

0 个答案:

没有答案