3

以下の方法を使用して、Webページ内の文字列を検索しています。プレーンテキストと一致します。ただし、次のコンテキストでは失敗します。

 <span>I</span> am searching for <b>text</b>

「amsearching」を検索すると一致しますが、「Iamsearching」を検索すると一致しません。以下は私が使用しているコードです:

function search(text)
{
    if (document.body.createTextRange) 
    {
        var textRange = document.body.createTextRange();
        while (textRange.findText(text)) 
        {
            textRange.execCommand("BackColor", false, "yellow");
            textRange.collapse(false);
        }
    }
}

これがフィドルです。 IE8では動作しません。ありがとう

4

1 に答える 1

1
<script type="text/javascript">
function findText(text) {
    $('*:contains('+text+')').css('background-color', 'Yellow');
});
</script>

<script type="text/javascript">
function findText(textBoxOrHtmlTagId, text) {
    $('#'+textBoxOrHtmlTagId+':contains('+text+')').css('background-color', 'Yellow');
});
$(document).ready(function(){
    $('#yourid').live('keydown', findText('yourid', $('#yourid').text());
});
</script>

動作するはずです:要するにjqueryを使用します

于 2011-12-05T22:33:32.010 に答える