如果已回答此问题,请道歉。很新鲜所以应该很容易。我无法让这个事件正常进行,而且我不应该在我出错的地方进行。如有任何帮助,我们将不胜感激!
我的html:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Final Project</title>
<script src="courseproject.js"></script>
</head>
<body>
<p>Type a number in the box below, choose either 1, 2, or 3.</p>
<input type="text" id="nbr" onblur="myFunction()">
</body>
<footer style="margin-left: 100px; margin-top: 100px">
<a href="main.html" class="menu">Home</a>
<a href="aboutme.html" class="menu">About</a>
<a href="futuregoals.html" class="menu">Goals</a>
<a href="contact.html" class="menu">Contact</a>
</footer>
</html>
我的外部 JS 函数:
funciton myFunction() {
var x = document.getElementById("nbr").value;
if (x == "1") {
alert("Solid Choice");
} else if (x == "2") {
alert("Not Bad");
} else if (x == "3") {
alert("Eh You Tried");
}
}
最佳答案
将 myFunction
公开到 window
:
window.myFunction = function() {
var x = document.getElementById("nbr").value;
if (x=="1") {
alert("Solid Choice");
} else if (x=="2") {
alert("Not Bad");
} else if (x=="3") {
alert("Eh You Tried");
}
}
参见https://codesandbox.io/s/adoring-bartik-h63x9?fontsize=14&hidenavigation=1&theme=dark
关于javascript - onblur 事件触发警报。完全菜鸟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59313128/