突出显示字符串搜索字符串

发布于 2024-12-05 03:39:34 字数 1624 浏览 1 评论 0原文

我使用下面的代码在文本区域中的文本中搜索文本输入中输入的字符串。我试图在搜索后突出显示文本区域中的字符串。我假设执行此操作的方法是 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>

I'm using the code below to search the text in a textarea for a string entered in a textinput. I'm trying to highlight the string in the textarea after it's been searched for. I assume the way to do this is selectRange(). I'm not sure how to find the endIndex for the second parameter of selectRange(). Below is what I have:

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);

EDIT

    <?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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热情消退 2024-12-12 03:39:34

不能只根据搜索字符串的长度来计算吗?

修改后的代码会是这样的:

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);
}

Update,响应原发帖者的代码更新;我继续进行测试。如果没有焦点,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>

Can't you just calculate it based on the length of the search string?

The modified code would be like this:

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);
}

Update, In response to the original poster's code update; I went ahead and tested. A TextAre will not display an item as selected if it does not have focus. Therefore the solution to get the selected range to highlight is to set focus to the textArea after the button is clicked. Here is code demonstrating this:

<?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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文