这是我第一次遇到这样的情况......我正在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
错误。