因此,我一直在从一些人那里获得帮助,了解如何解决我希望如何能够单击按钮并让它自动选择我网站上的搜索框的问题。
好吧,人们告诉我使用 Javascript 来实现点击,我被告知使用这段代码:
function setFocus() {
document.getElementById("myTextbox").focus();
}
但我不知道如何将它与我的 html 正确结合。在 Google 上环顾四周,一切对我来说都不是拼凑起来的..
我的 HTML:
<html>
<head>
<title>DLL Download Database</title>
<meta name="description" content="Need a .DLL file? Don't worry, I am sure we have a DLL available for you!">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="main.css">
<script type="text/javascript">
function setFocus() { document.getElementById("search").focus(); }
</script>
</head>
<body>
<div class="my_container cf">
<div class="headerbuttons"><a href="http://dll-download.us/" style="text-decoration: none; color:#000000;padding:3px;display:block;"><p>HOME</p></a></div>
<div class="headerbuttons"><a href=""style="text-decoration: none; color:#000000;padding:3px;display:block;"><p>HOW TO INSTALL</p></a></div>
<div class="headerbuttons"><p style="text-decoration: none; color:#000000;padding:3px;display:block;" onClick="setFocus(search);">CAN'T FIND A FILE?</p></div>
<div class="headerbuttons"><a href="http://www.dll-files.com/get-fixer/"style="text-decoration: none; color:#000000;padding:3px;display:block;"><p>DLL SOFTWARE FIXER</p></a></div>
</div>
<h2 class="homepage-start"><strong>WELCOME TO DLL DOWNLOAD.US!</strong></h2>
<div class="body-main2">
<form method="get" action="/search" id="search">
<input name="q" type="text" size="40" placeholder="Search for a DLL" />
</form>
</div>
</body>
</html>
最佳答案
请在您的 html 中进行以下更改
HTML
<div class="body-main2">
<form method="get" action="/search" id="search">
<input id="searchbox" name="q" type="text" size="40" placeholder="Search for a DLL"/>
</form>
</div>
<div class="headerbuttons">
<a style="text-decoration: none;
color:#000000;padding:3px;display:block;" onclick="setFocus();">
<p>CAN 'T FIND A FILE?</p>
</a>
</div>
JavaScript
<script type="text/javascript">
function setFocus() {
document.getElementById("searchbox").focus();
}</script>
它工作正常!!!看这个working JSFIDDLE Demo
关于javascript - 如何在 HTML 页面上使用这个 javascript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20779569/