簡體   English   中英

使用JQuery在textarea中獲取光標位置和突出顯示的文本

[英]Fetching cursor location and highlighted text in textarea using JQuery

在表單中有一個textarea我試圖做幾件事:

  • 獲取文本區域內光標的當前位置
  • 獲取textarea中的當前選擇
  • 在當前光標位置插入一些文本
  • 用其他文字替換當前選擇

由於我已經在使用JQuery,我更喜歡一種能夠順利運行的解決方案。 任何指針如何實現上述將是值得贊賞的。

有很多jQuery插件。 這是我以前用過的好文章:

http://plugins.jquery.com/project/a-tools


要在文本區域內獲取光標的當前位置:

$("textarea").getSelection().start;

要獲取textarea中的當前選擇:

$("textarea").getSelection();

這會返回一個這樣的對象:

{
    start: 1, // where the selection starts
    end: 4, // where the selection ends
    length: 3, // the length of the selection
    text: 'The selected text'
}

要在當前光標位置插入一些文本:

$("#textarea").insertAtCaretPos("The text to insert");

要通過其他文本替換當前選擇:

$("#textarea").replaceSelection('This text will replace the selection');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM