最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Body onclick go to textarea - Stack Overflow

programmeradmin4浏览0评论

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!!

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!!

Share Improve this question asked Oct 6, 2012 at 18:13 Kyle MorganKyle Morgan 6901 gold badge11 silver badges21 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

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

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

Vanilla JS:

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

without jQuery (pure JS):

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

评论列表(0)

  1. 暂无评论