I have a <p>
containing text. When the <p>
is clicked on, I create a <textarea>
containing the text from the <p>
. Is it possible to calculate where in the <p>
's text the click occurred, and move the <textarea>
's cursor to that same point?
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to fix IE ClearType + jQuery opacity problem i
- void before promise syntax
I don't believe so, no. The DOM just knows what containing element received the click event, it doesn't distinguish between pieces of text within the containing element unless they are elements themselves. And I doubt you want to wrap every character in your text with its own element tag :)
I'm guessing this is going to take a fair amount of fiddling to get right, and you won't be able to get it exactly right. But you'll probably want to use event.clientX and event.clientY.
EDIT -- didn't know about this stuff when I replied. Looks pretty possible to get it exactly correct. http://www.quirksmode.org/dom/range_intro.html
An alterntive idea: style the textarea so it looks like plain text, and re-style it to look like a form field when it gets clicked.
Hope this simple example helps:
Or you can use third-party JS library like jQuery - see this example.