这里的所有代码都完美无缺,我只想让用户只需按 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>
答案 0 :(得分:1)
var inp = document.getElementById("textmoney");
inp.addEventListener("keydown", function (e) {
if (e.keyCode === 13) { //checks whether the pressed key is "Enter"
moneyFunction();
}
});
答案 1 :(得分:0)
$('.div').click()
如果页面上包含jquery,你的keydown事件内部会调用onclick。