Click on
to focus <input>

2020-02-26 04:05发布

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?

6条回答
Fickle 薄情
2楼-- · 2020-02-26 04:43

Try this with jquery:

$('#yourdiv').click(function() {
     $('#yourfield').focus();
});
查看更多
虎瘦雄心在
3楼-- · 2020-02-26 04:51
  1. USING HTML ("for") :

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

 <label for="idname">Click Here to Reach Input<label>
 <input id="idname" type="text">
  1. USING jQuery ("focus")

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

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

查看更多
甜甜的少女心
4楼-- · 2020-02-26 04:56

Try this :

<input id="myInput" />
<div onclick="document.getElementById('myInput').focus(); return false;"></div>
查看更多
对你真心纯属浪费
5楼-- · 2020-02-26 04:57
  1. using javascript (call .focus() on the input element)
  2. using wrapped around your div (makes only sense if the div is the label)
查看更多
虎瘦雄心在
6楼-- · 2020-02-26 05:03

I don't see a reason why you need JS to do this when such feature is already provided in HTML.

<label for="YOURID">The clickable region<label>
<input id="YOURID" type="text" />
查看更多
ゆ 、 Hurt°
7楼-- · 2020-02-26 05:06
$('div').click(function() {
    $(this).find('input[type="text"]').focus();
});​

LIVE DEMO

查看更多
登录 后发表回答