问题描述:

I want a text input selected (as if I had clicked on ) when I click on another element on the page. I can't figure out how to do it ?

网友答案:

You are looking for this: pure js way

<img src="http://placehold.it/350x150" 
onclick="document.getElementById('target').focus(); return false;">

<input type ="text" id="target"/>
网友答案:

Assuming that your input field has id #input then solution would be

// Wrapper function needed to ensure that DOM elements have been already rendered.
(function() {

  $( '#input' ).focus(); // jQuery

  document.getElementByID( 'input' ).focus(); // VanillaJS

})();
网友答案:

Try this. I have not tried it though. It's worth noting that it is based on jquery.

//set focus by default
$("your_input").focus();
//if the focus is lost set it again
$("div").focusout(function(){
    $("your_input").focus();
});
相关阅读:
Top