Javascript 点击 <div> 聚焦 <input>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10164015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Click on <div> to focus <input>
提问by Erik
I have a series of input
elements each with a few <div>
tags separate.
I like to have if where I can click
on any of the <div>
and it will focus
on an input
.
Is this possible and if so can anyone give ideas on script?
我有一系列input
元素,每个元素都有几个<div>
单独的标签。
我喜欢如果我可以click
在任何一个上拥有<div>
它,它就会focus
在一个input
.
这可能吗,如果可以的话,有人可以就剧本提出想法吗?
采纳答案by Adam Seabridge
Try this with jquery:
用 jquery 试试这个:
$('#yourdiv').click(function() {
$('#yourfield').focus();
});
回答by apnerve
I don't see a reason why you need JS
to do this when such feature is already provided in HTML
.
我不明白你为什么需要理由JS
时,在已经提供这种功能来做到这一点HTML
。
<label for="YOURID">The clickable region<label>
<input id="YOURID" type="text" />
回答by d4rkpr1nc3
Try this :
尝试这个 :
<input id="myInput" />
<div onclick="document.getElementById('myInput').focus(); return false;"></div>
回答by gdoron is supporting Monica
回答by Niko Sams
- using javascript (call .focus() on the input element)
- using wrapped around your div (makes only sense if the div isthe label)
- 使用 javascript(在输入元素上调用 .focus())
- using 环绕你的 div (只有当 div是标签时才有意义)
回答by Ashutosh Tiwari
USING HTML ("for") :
HTML provides this functionality. Using "for" "id" you can easily achieve it.
使用 HTML (" for") :
HTML 提供了此功能。使用“ for”“ id”可以轻松实现。
<label for="idname">Click Here to Reach Input<label> <input id="idname" type="text">
<label for="idname">Click Here to Reach Input<label> <input id="idname" type="text">
- USING jQuery ("focus")
- 使用 jQuery(“焦点”)
$(".classname").click(function(){
$("input").focus(); })
$(".classname").click(function(){
$(“输入”)。焦点();})