Man*_*kla 1 html javascript css
var input = document.getElementById("search");
function showTrue() {
document.getElementById("output").innerHTML = "It's true";
}
function showFalse() {
document.getElementById("output").innerHTML = "It's false";
}
// Get the input field
// Execute a function when the user releases a key on the keyboard
input.addEventListener("keyup", function(event) {
// Cancel the default action, if needed
event.preventDefault();
// Number 13 is the "Enter" key on the keyboard
if (event.keyCode === 13) {
// Trigger the button element with a click
showFalse();
}
});Run Code Online (Sandbox Code Playgroud)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="author" content="colorlib.com">
<link href="https://fonts.googleapis.com/css?family=Poppins:400,800" rel="stylesheet" />
<link href="css/main.css" rel="stylesheet" />
<script type="text/javascript" src="js/main.js">
</script>
</head>
<body style="background-color:black">
<div class="s006">
<form>
<fieldset>
<legend>What are you looking for?</legend>
<div class="inner-form">
<div class="input-field">
<button id="trueButton" onclick="showTrue()" class="btn-search" type="button">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
<path d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
</svg>
</button>
<input id="search" type="text" placeholder="Test you news here" value="" />
</div>
</div>
</fieldset>
<p style="font-size: 36px;
color: #fff;
font-weight: 800;
text-align: center;
margin-bottom: 59px;" id="output"></p>
</form>
</div>
</body><!-- This templates was made by Colorlib (https://colorlib.com) -->
</html>Run Code Online (Sandbox Code Playgroud)
我想按下输入按钮它应该显示,it's flase但然后按下输入按钮它首先显示输出,然后刷新页面任何想法为什么和建议我做什么来防止它?
我只是想在输入键被按下时它应该在搜索字段下面显示"它是假的"
当按键事件的默认操作通过时,页面将被替换,因此您需要为keypress事件添加一个侦听器,并.preventDefault()在enter按下时调用它们.(keyup事件不需要preventDefault()任何东西,AFAIK)
但是还有另一个问题; 它看起来像你的script标签head,并不等待文件填充运行.给它defer属性.
<script type="text/javascript" src="js/main.js" defer>
Run Code Online (Sandbox Code Playgroud)
当脚本标记具有该defer属性时,它将等待在运行之前加载文档.
var input = document.getElementById("search");
function showTrue() {
document.getElementById("output").innerHTML = "It's true";
}
function showFalse() {
document.getElementById("output").innerHTML = "It's false";
}
// Get the input field
input.addEventListener("keypress", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
}
});
// Execute a function when the user releases a key on the keyboard
input.addEventListener("keyup", function(event) {
// Number 13 is the "Enter" key on the keyboard
if (event.keyCode === 13) {
// Trigger the button element with a click
showFalse();
}
});Run Code Online (Sandbox Code Playgroud)
<body style="background-color:black">
<div class="s006">
<form>
<fieldset>
<legend>What are you looking for?</legend>
<div class="inner-form">
<div class="input-field">
<button id="trueButton" onclick="showTrue()" class="btn-search" type="button">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
<path d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
</svg>
</button>
<input id="search" type="text" placeholder="Test you news here" value="" />
</div>
</div>
</fieldset>
<p style="font-size: 36px;
color: #fff;
font-weight: 800;
text-align: center;
margin-bottom: 59px;" id="output"></p>
</form>
</div>
</body>Run Code Online (Sandbox Code Playgroud)