我有一個textArea和一個列表。當用戶雙擊列表項目時,所選項目的標籤應插入到textarea中。在textArea中選擇文本時,應該替換它,否則只需將文本插入到插入點的現有文本中。插入文本到flex 3 textarea
我已經設法得到文本和一切,我只是無法設法插入它的插入點。有誰知道如何做到這一點?
我有一個textArea和一個列表。當用戶雙擊列表項目時,所選項目的標籤應插入到textarea中。在textArea中選擇文本時,應該替換它,否則只需將文本插入到插入點的現有文本中。插入文本到flex 3 textarea
我已經設法得到文本和一切,我只是無法設法插入它的插入點。有誰知道如何做到這一點?
它實際上不是JavaScript,但Adobe Flex的3.感謝您的幫助,雖然,它沒有把我在正確的方向。這是它在Flex 3中完成的工作方式:
var caretStart:int = textArea.selectionBeginIndex;
var caretEnd:int = textArea.selectionEndIndex;
textArea.text = textArea.text.substring(0,caretStart)
+ newText
+ textArea.text.substr(caretEnd);
您可以使用txtarea.selectionStart和txtarea.selectionEnd來獲取選定的文本位置。
之後,您刪除txt並添加新的選定文本。
我不知道太多關於JavaScript的,所以我寫了它U.
你可以用關鍵字在谷歌搜索: 「的Javascript所選文本的TextArea」 「的Javascript在位置添加文本」
示例代碼: function insertAtCursor(myField,myValue){ // IE支持 if(document.selection){0} {0} {0} {0} myField.focus(); sel = document.selection.createRange(); sel.text = myValue; (myField.selectionStart || myField.selectionStart =='0'){ var startPos = myField.selectionStart; var endPos = myField.selectionEnd; myField.value = myField.value.substring(0,startPos) + myValue + myField.value.substring(endPos,myField.value.length); } else { myField.value + = myValue; }
caretPos = doGetCaretPosition(myField);
alert(caretPos);
setCaretPosition(myField,caretPos-3);
}
如果您沒有現有的HTML格式,接受的答案將很有用。就我而言,我在編輯器中插入了一個新按鈕,用戶可以點擊該按鈕放入關鍵詞。我一直在失去所有的HTML格式,直到我在實際的階級周圍挖,用TextRange對象雙面:
public function keyWord_Click(event:Event) : void
{
var caretStart:int = txtEditor.textArea.selectionBeginIndex;
var caretEnd:int = txtEditor.textArea.selectionEndIndex;
var newText : String = "[[[KEYWORD]]]";
var tf:TextRange = new TextRange(txtEditor,true,caretStart,caretEnd);
tf.text = newText;
}
對這種做法的好處是,你也可以根據需要應用條件格式到TextRange對象。
謝謝裝人!我浪費了兩天的時間尋找這個修復! – 2017-05-03 23:24:15
有沒有想辦法做到這一點,而不會丟失現有文本的HTML?我非常肯定,如果有的話,上面的代碼片段會剝離現有的HTML。 – 2010-09-23 23:07:09
沒關係。我只是想出了一個辦法。我會在下面發帖... – 2010-09-23 23:24:29