javascript - 触发输入键时更改按钮样式
问题描述
我是 HTML/CSS 和 JavaScript 的初学者。
我想button
通过ENTER按键触发。问题是当我悬停按钮或单击它时,它会更改背景颜色,而输入“ENTER”键不会。当我按下 ENTER 键时,我想更改按钮的背景颜色。
document.getElementById("password").addEventListener("keyup", function(event) {
event.preventDefault();
if (event.keyCode === 13) {
document.getElementById("buttonpw").click();
}
});
function mypassword() {
var x = document.getElementById("password").value;
if (x === "SolitaryRJin") {
alert("It's not for You. Bye bye!!");
} else {
alert("It's not for You. Bye bye!!");
}
}
button {
position: absolute;
z-index: 100;
width: 149px;
height: 43px;
border: 1px solid #fff;
background: #000;
font-family: 'Roboto', sans-serif;
color: #fff;
font-size: 16px;
border-radius: 22px;
transition: 0.5s;
cursor: pointer;
margin: 0;
position: absolute;
top: 80%;
left: 50%;
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
button:hover {
color: #000;
background: #fff;
}
button:focus {
outline-width: 0;
}
<input id='password' class='pass' onfocus="" class='inc1' type="password" name="Password" placeholder='Password' value="">
<button id='buttonpw' onclick="mypassword()">Sign In</button>
解决方案
在里面添加这个if
-document.getElementById("buttonpw").setAttribute("style", "color: #000;background: #fff;");
像这样:
if (event.keyCode === 13) {
document.getElementById("buttonpw").setAttribute("style", "color: #000;
background: #fff;");
document.getElementById("buttonpw").click();
}
推荐阅读
- azure-service-fabric - 通过 RDP 远程进入 SF 节点的用户名和密码
- grails - Grails 3记录非字符串对象作为第一个参数
- orange - 如何在橙色数据挖掘的 Python 脚本中删除具有缺失值的实例或行?
- python - pipenv 坚持安装 Python 3.6
- r - R:具有多值预测的 rpart
- javascript - 如何使用 rollup.js 将所有依赖项嵌入到一个胖目标包中?
- dart - 如何访问飞镖中的嵌套字典?
- amazon-web-services - 用于 AWS 类型的 aws_api_gateway_integration 的 uri(集成)到 DynamoDb
- cakephp - 无法配置 Header 安全中间件 CakePHP
- android - 如何在 Imageview 下方放置一个按钮