use window.getSelection get selected or cursor located text line from textarea

左心房为你撑大大i 提交于 2020-01-06 03:40:10

问题


I am dynamically filling textarea using Ajax call's. In my code, I want to get the cursor's current line content using window.getSelection. I tried following code,

var range = window.getSelection().toString;
alert (typeof(range));

But, It returns function as alert Message. or Any other better way, to get the cursor's current line content from textarea that code need to support in all browser's.? Once I get the current line content I will find out line number and update new content on it.


回答1:


Firstly, textareas have a different selection API from regular content: use selectionStart and selectionEnd properties of the textarea rather than window.getSelection().

Secondly, getting the current line requires some creative coding if you're accounting for the browser's auotmatic wrapping of content. I've seen a few questions about this in Stack Overflow. Here's one example:

finding "line-breaks" in textarea that is word-wrapping ARABIC text



来源:https://stackoverflow.com/questions/19953220/use-window-getselection-get-selected-or-cursor-located-text-line-from-textarea

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