gpt4 book ai didi

javascript - 按 Enter 时如何调用 div 的 onclick 处理程序?

转载 作者:行者123 更新时间:2023-12-03 10:46:05 24 4
gpt4 key购买 nike

这里的所有代码都工作得很好,我只是想让用户更容易,让他/她只需点击 Enter 即可执行该功能,而不是必须单击div 激活 moneyFunction()

这是我的代码:

<!DOCTYPE html>
<html>

<head>
<title> YearlyDough </title>
<link type="text/css" rel="stylesheet" href="money.css">
</head>

<body>
<p id="header"> Enter yearly income </p>
<input type="text" id="textmoney">
<div onclick="moneyFunction()" id="moneydiv"> <p id="divtext">Calculate</p> </div>
<p id="demo"></p>
<p id="liar"></p>

<div onclick="reloadFunction()" id="reload"> Redo </div>

<a href="aboutmoney.html" id="about"> About </a>


<script>
function moneyFunction() {
var money = document.getElementById('textmoney').value;
var dailyE = money/365;

document.getElementById('demo').innerHTML = ("$" + dailyE + " " + "per day");

if ( document.getElementById('textmoney').value == 0) {
document.getElementById('demo').innerHTML = "ERROR";
}
if ( document.getElementById('textmoney').value > 100000000000) {
document.getElementById('liar').innerHTML = "I know you aint make that much.";
} else {
document.getElementById('liar').innerHTML = "";
}
}

function reloadFunction() {
location.reload();
}
</script>
</body>
</html>

最佳答案

var inp = document.getElementById("textmoney");
inp.addEventListener("keydown", function (e) {
if (e.keyCode === 13) { //checks whether the pressed key is "Enter"
moneyFunction();
}
});

关于javascript - 按 Enter 时如何调用 div 的 onclick 处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28568104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com