我正在使用下面的代码在 textarea 中的文本中搜索在 textinput 中输入的字符串。我试图在搜索后突出显示 textarea 中的字符串。我认为这样做的方法是selectRange()
. 我不确定如何找到第二个参数的 endIndex selectRange()
。以下是我所拥有的:
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>