Body onclick go to textarea

拈花ヽ惹草 提交于 2019-12-11 07:58:08

问题


This may not seem like a useful script, but I need something that will basically allow the user to click anywhere on the page and the cursor will go to the textarea.

How I imagine this happening is by having but I don't know that much about javascript to do this.

When the user clicks on the body, paragraph, image, or anything inside the body the cusor will automatically go to the textarea and he will be able to type in it.

I know this doesn't seem like it would be useful for anything, but trust me, I have a use for it. Thanks for the help!!


回答1:


Easy with jquery. But how useful, I don't know.

$('body').on('click', function(){
  $('textarea').focus();
});



回答2:


Vanilla JS:

document.onclick = function(){ document.getElementById('yourIDHere').focus(); }



回答3:


without jQuery (pure JS):

document.body.addEventListener('click', 
function(){
  document.getElementsByTagName('textarea')[0].focus();
});


来源:https://stackoverflow.com/questions/12762472/body-onclick-go-to-textarea

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