2011-09-17 154 views
0

我正在使用下面的代碼來搜索文本輸入中輸入的字符串的textarea中的文本。我正在嘗試在textarea搜索後突出顯示字符串。我假設這樣做的方式是selectRange()。我不知道如何找到第二個參數selectRange()的endIndex。下面是我有:突出顯示字符串搜索字符串

protected function searchBtn_clickHandler(event:MouseEvent):void 
{ 
    text = mainTextField.text; 
    search_Str = searchTxt.text; 

    var search_result:int = text.search(search_Str); 
    trace(search_result); 

編輯

<?xml version="1.0" encoding="utf-8"?> 
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
     xmlns:s="library://ns.adobe.com/flex/spark" title="HomeView"> 
    <fx:Declarations> 
     <!-- Place non-visual elements (e.g., services, value objects) here --> 
    </fx:Declarations> 
    <s:titleContent> 
     <s:TextInput id="searchTxt"/> 
     <s:Button label="Button" click="searchBtn_clickHandler(event)"/> 
    </s:titleContent> 
    <s:TextArea id="mainTextField" x="33" y="35" width="544" height="444"/> 
    <fx:Script> 
     <![CDATA[ 
      public var text:String; 
      public var search_Str:String; 

      protected function searchBtn_clickHandler(event:MouseEvent):void 
     { 
      text = mainTextField.text; 
      search_Str = searchTxt.text; 

      var search_result:int = text.search(search_Str); 
      trace(search_result); // Traces correct int values 
      trace(mainTextField.selectRange(search_result,search_result+search_Str.length)); // Traces "undefined" 
     } 
     ]]> 
    </fx:Script> 
</s:View> 

回答

2

你就不能計算它基於搜索字符串的長度?

修改後的代碼會是這樣的:

protected function searchBtn_clickHandler(event:MouseEvent):void 
{ 
    text = mainTextField.text; 
    search_Str = searchTxt.text; 

    var search_result:int = text.search(search_Str); 
    trace(search_result); 
    mainTextField.selectRange(search_result,search_result+search_Str.length); 
} 

更新,針對樓主的代碼更新;我繼續測試。如果沒有焦點,TextAre將不會顯示選中的項目。因此,讓選定範圍突出顯示的解決方案是在單擊按鈕後將焦點設置爲textArea。這裏是代碼演示這個:

<?xml version="1.0" encoding="utf-8"?> 
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
     xmlns:s="library://ns.adobe.com/flex/spark" title="HomeView"> 
    <fx:Declarations> 
     <!-- Place non-visual elements (e.g., services, value objects) here --> 
    </fx:Declarations> 

    <s:titleContent> 
     <s:TextInput id="searchTxt" text="Quick" /> 
     <s:Button label="Button" click="searchBtn_clickHandler(event)"/> 
    </s:titleContent> 
    <s:TextArea id="mainTextField" x="33" y="35" width="544" height="444" text="The Quick Brown Fox Jumped Over The Lazy Dogs"/> 
    <fx:Script> 
     <![CDATA[ 
      public var text:String; 
      public var search_Str:String; 

      protected function searchBtn_clickHandler(event:MouseEvent):void 
      { 
       mainTextField.setFocus(); 
       text = mainTextField.text; 
       search_Str = searchTxt.text; 

       var search_result:int = text.search(search_Str); 
       trace(search_result); // Traces correct int values 
       trace(search_result+search_Str.length); 
//    trace(mainTextField.selectRange(search_result,search_result+search_Str.length)); // Traces "undefined" 
       mainTextField.selectRange(search_result,search_result+search_Str.length) 
      } 
     ]]> 
    </fx:Script> 


</s:View> 
+0

謝謝:)。這太好了 – RapsFan1981

+0

我重新打開了這個,因爲上面的代碼使用跟蹤,它返回開始和結束整數,但實際的字符串不會突出顯示。我錯過了什麼? – RapsFan1981

+0

我不明白「跟蹤工作」的含義。你能提供一個完整的可運行樣本嗎? – JeffryHouser