[英]How to use this javascript on HTML page?
So I have been getting help from some as to how I could fix the issue about how I want to be able to click a button, and have it instead auto select the search box on my site. 所以我一直在寻求一些关于如何解决我希望如何能够点击按钮的问题的帮助,并让我自动选择我网站上的搜索框。
Well, people are telling me to use Javascript for the click to do it and I was told to use this code: 好吧,人们告诉我使用Javascript进行点击,我被告知要使用这段代码:
function setFocus() {
document.getElementById("myTextbox").focus();
}
But I don't know how to properly combine it with my html. 但我不知道如何正确地将它与我的HTML结合起来。 Looked around on Google and everything isn't piecing together for me..
环顾谷歌,一切都没有为我拼凑..
My HTML: 我的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>
Please make the following changes in your html 请在您的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 JavaScript的
<script type="text/javascript">
function setFocus() {
document.getElementById("searchbox").focus();
}</script>
Its working fine !!! 它的工作正常!!! See this working JSFIDDLE Demo
看到这个工作JSFIDDLE演示
try 尝试
onClick="javascript:setFocus();" // no parameter
or more simply 或者更简单
onClick="setFocus();" // no parameter
BTW, you should be setting the focus on the input tag not the form
, so change the id of the input tag to be id="search"
and of course remove it from the form
. 顺便说一句,您应该将焦点设置在输入标记而不是
form
,因此将输入标记的id更改为id="search"
,当然将其从form
删除。
instead of calling setFocus(search)
而不是调用
setFocus(search)
have you tried calling only setFocus()
你试过只调用
setFocus()
吗?
in 在
`<div class="headerbuttons"><p style="text-decoration: none;color:#000000;padding:3px;display:block;"
onClick="setFocus(search);">CAN'T FIND A FILE?</p></div>`
EDIT: 编辑:
remove id = search
from the form
element and set it on the text
element 从
form
元素中删除id = search
并将其设置在text
元素上
<form method="get" action="/search" >
<input name="q" type="text" size="40" placeholder="Search for a DLL" id="search" />
</form>
onClick="setFocus(search);
remove "search" inside the braces 删除大括号内的“搜索”
set id="search"
for text field 为文本字段设置
id="search"
You were not calling the function setFocus(). 你没有调用函数setFocus()。 Also, there was no ID assigned to the textbox.
此外,没有为文本框分配ID。
HTML: HTML:
<input name="q" id="search_box" type="text" size="40" placeholder="Search for a DLL" />
Javascript: 使用Javascript:
document.getElementById("search_box").focus();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.