I have a modal window which helps formatting text. I have multiple textareas on the window. The modal should not be attached to a specific textarea, so when I press an Icon in the modal window, I need to insert a string/emoticon etc where-ever the cursor is currently. My question, How do I know in which element (textarea/input/whatever) the cursor is currently in?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
The latest version of all browsers support document.activeElement. That will tell you which field currently has focus within that window (that's where the cursor is). Then, you'll need to know how to insert text at the current cursor position. The following function does just that.
Therefore, from your popup, your button handler should call the following method
There doesn't seem to be a property like
isfocused
that you can just check in order to determine whether a particular text field has focus. However, you could create an event handler for theonFocus
event for each of the text boxes, and have it record ID of the newly-focused textbox in a variable so that you can check it later.Also, you may be interested in this tutorial, which tells you how to insert text at the current cursor position within a given field (once you've determined which field is focused, e.g. using the above method).