Click on <div> to focus <input>

匆匆过客 提交于 2019-11-30 13:39:52

问题


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?


回答1:


Try this with jquery:

$('#yourdiv').click(function() {
     $('#yourfield').focus();
});



回答2:


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" />



回答3:


Try this :

<input id="myInput" />
<div onclick="document.getElementById('myInput').focus(); return false;"></div>



回答4:


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

LIVE DEMO




回答5:


  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:


  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(); })



来源:https://stackoverflow.com/questions/10164015/click-on-div-to-focus-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!