我被困在为什么我将注意力集中在输入上时,它不显示控制台日志消息的原因,不像它为空还是不为空,而是显示该消息。我使用的活动有误吗?
(只有我的代码的一部分,如果需要更多详细信息,请告诉我)
HTML
<input id='input' placeholder="President Name" type="text">
JS
function message() {
var presidentInput = document.getElementById('input').value;
if (presidentInput === "") {
console.log('Please enter something in the input');
} else if (presidentInput !== "" || document.getElementById('input').onfocus){
console.log('Great job');
}
}
message();
最佳答案
var input = document.getElementById('input');
jQuery.input.focus(function(){
if(input.value === ""){
console.log('Please enter something in the input');
else{
console.log('Great job');
}
});
也许这段代码不正确。
但是,关键是在输入TAG上使用事件监听器。
input.addEventListener("focus", function(){
if(input.value === ""){
console.log('Please enter something in the input');
else{
console.log('Great job');
}
});
关于javascript - 专注于输入,以显示是否有条件的控制台日志消息?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45604353/