如何在textarea中使用window.find()函数? [英] How to use window.find() function in textarea?

查看:115
本文介绍了如何在textarea中使用window.find()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我需要突出显示作为输入输入的文本。在javascript中使用window.find()。如果内容超出了textarea window.find()工作正常。但如果内容在textarea内部,它似乎无法正常工作。只是第一次出现的字符串只是变得明亮,我没有得到所有在textarea内突出显示的字符串。



这是我的代码。



Hi,

I need to highlight the text which is entered as an input.Am using window.find() in javascript.If the content is outside of the textarea window.find() works fine.But if the content is inside of the textarea it seems like not working. just first occurence of string only getting hightlighted,am not gettin all of the occurences of string hightlighted inside textarea.

Here is my code.

<pre lang="HTML"><pre lang="HTML"></pre><head>
    <script type="text/javascript">
        function FindNext () {
            var str = document.getElementById ("findInput").value;
            if (str == "") {
                alert ("Please enter some text to search!");
                return;
            }

            if (window.find) {        // Firefox, Google Chrome, Safari
                var found = window.find (str);
                if (!found) {
                    alert ("The following text was not found:\n" + str);
                }
            }
            else {
                alert ("Your browser does not support this example!");
            }
        }
    </script>
</head>
<body>
    <div>LaLa, Lala, laLa , lala, lalala, tralala, some other text</div>
    <br />
    <input type="text" id="findInput" value="lala" size="20" />
    <button onclick="FindNext ();">Find Next</button>
</body>
</pre>









这里的代码只用于div ..我也需要在textarea里面工作,我希望所有发现的字符串突出显示。

感谢阅读





Here the code is for only div..I need this working inside textarea also and I want to all the occurences of found string highlighted.
Thanks for reading

推荐答案

首先,使用所有浏览器都不具备的API并不是很有用。另请参阅Mozilla警告(错误672395):

https: //developer.mozilla.org/en-US/docs/Web/API/Window.find [ ^ ],

https://bugzilla.mozilla.org/show_bug.cgi?id=672395 [ ^ ]。



但是,如果你想忽略可怜的IE,它可能会工作;太糟糕了,它不是Javascript-in-a-Web浏览器环境的标准部分。



现在,它不适用于在输入控件中输入的数据,所以什么?您需要使用属性 value 和字符串搜索方法分别在这些值中查找字符串:

http://www.w3schools.com/jsref/jsref_search.asp [ ^ ],

http://www.w3schools.com/jsref/jsref_indexof.asp [ ^ ]。



您还可以使用正则表达式搜索: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search [ ^ ]。



-SA
First of all, it's not very useful to use the API which is not available in all browsers. See also the Mozilla warning (bug 672395):
https://developer.mozilla.org/en-US/docs/Web/API/Window.find[^],
https://bugzilla.mozilla.org/show_bug.cgi?id=672395[^].

However, if you wish to ignore the poor IE, it might work; too bad it's not a standard part of Javascript-in-a-Web-browser environment.

Now, it won't work with data entered in input controls, so what? You will need to find the strings in those values separately, using the property value and string search methods:
http://www.w3schools.com/jsref/jsref_search.asp[^],
http://www.w3schools.com/jsref/jsref_indexof.asp[^].

You can also use Regular Expression search: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search[^].

—SA


这篇关于如何在textarea中使用window.find()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆