gpt4 book ai didi

Javascript 在新选项卡中输入 key 打开页面

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

我正在使用搜索框,并且尝试了多种修复,所有这些修复都反射(reflect)在代码中。当您单击搜索按钮时,搜索栏会工作并将您带到搜索页面,但是如果您按 Enter 键,它只会将当前页面的刷新版本拉入新选项卡。有人知道解决这个问题的方法吗?

<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<link rel="stylesheet" type="text/css" href="Main.css">
</head>
<body></body>
<div class="body"></div>
<div class="grad"></div>
<div class="header">
<div>Abuse<span>Caboose</span></div>
</div>
<br />
<br />
<br />
<br />
<a href="login.html">Login</a>
<br />
<br />
<a href="Media_Page.html">Media</a>
<br />
<br />
<a href="Bios.html">Our Team List </a>
<br />
<br />
<a href="ContactUs.html">Contact Us! </a>
<br />
<br />
<img src="http://res.cloudinary.com/lmn/image/upload/e_sharpen:150,f_auto,fl_lossy,q_80/v1/gameskinny/5edfe8862832766bd1c9a38e6a9821eb.jpg" alt="I am Support" title="Image" width="400" height="200" />
<br />
<br />
<br />
<br />
<div id="tfheader">
<form target="_blank">
<div><input type="text" placeholder="Search Google" onchange="urlinput(event)" onkeypress="return searchit()"></div>
<br />
<a id="searchbutton" href="" onclick="searchit()" target="_blank" onkeypress="return searchit()">Search</a>
</form>
</div>
<script>
const baseUrl = "https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=";
var userurl;
var enterurl;
function urlinput(event){
var text = event.target.value;
text = text.replace(" ", "+");
userurl = text;
var searchButton = document.getElementById('searchbutton').href="baseUrl";
}
function searchit()
{
document.getElementById("searchbutton").href= baseUrl + userurl;
}
</script>
</html>

最佳答案

您的文本框位于表单内,当您按 Enter 键时,默认情况下会提交表单。通过处理 onsubmit 函数来覆盖表单的默认提交行为。如果从此函数返回 false,则表单将不会提交。

<form target="_blank" onsubmit='return handleSubmit();'>
<div><input type="text" placeholder="Search Google" onchange="urlinput(event)" onkeypress="return searchit()"></div>
<br />
<a id="searchbutton" href="" onclick="searchit()" target="_blank" onkeypress="return searchit()">Search</a>
</form>


<script>
function handleSubmit() {
return false;
}
</script>

关于Javascript 在新选项卡中输入 key 打开页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41897287/

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