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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:08:01  来源:igfitidea点击:

Click on <div> to focus <input>

javascriptjqueryhtmlinputonclick

提问by Erik

I have a series of inputelements each with a few <div>tags separate.
I like to have if where I can clickon any of the <div>and it will focuson 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 JSto 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

$('div').click(function() {
    $(this).find('input[type="text"]').focus();
});?

LIVE DEMO

现场演示

回答by Niko Sams

  1. using javascript (call .focus() on the input element)
  2. using wrapped around your div (makes only sense if the div isthe label)
  1. 使用 javascript(在输入元素上调用 .focus())
  2. using 环绕你的 div (只有当 div标签时才有意义)

回答by Ashutosh Tiwari

  1. USING HTML ("for") :

    HTML provides this functionality. Using "for" "id" you can easily achieve it.

  1. 使用 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">
  1. USING jQuery ("focus")
  1. 使用 jQuery(“焦点”)

$(".classname").click(function(){

$("input").focus(); })

$(".classname").click(function(){

$(“输入”)。焦点();})