javascript - 如何在 HTML 页面上使用这个 javascript?

标签 javascript jquery html

因此,我一直在从一些人那里获得帮助,了解如何解决我希望如何能够单击按钮并让它自动选择我网站上的搜索框的问题。

好吧,人们告诉我使用 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/

相关文章:

javascript - 引用这个来轻松确定范围是好主意还是坏主意?

javascript - 仅当 div 在表单内时提交按钮才有效 - React

javascript - typescript 中的对象解构

javascript - 在 javascript 中真的有一个很好的理由从不检查 boolean true || false 例如 if(var){}else{}?

jquery - 如何在 Jquery 中将浮点值四舍五入到最接近的整数

jquery - 使用 casperjs 根据文本点击 html 元素

html - 如何让父级(相对)根据子级(绝对)div 展开

html - margin-left 在 div 的右侧添加空间

javascript - 链接到 JavaScript 的最佳方式

javascript - 如何在 Textmate 中创建自定义符号列表项?