Find text string using jQuery?

后端 未结 7 1390
暖寄归人
暖寄归人 2020-11-22 07:38

Say a web page has a string such as \"I am a simple string\" that I want to find. How would I go about this using JQuery?

相关标签:
7条回答
  • 2020-11-22 07:54

    this function should work. basically does a recursive lookup till we get a distinct list of leaf nodes.

    function distinctNodes(search, element) {
        var d, e, ef;
        e = [];
        ef = [];
    
        if (element) {
            d = $(":contains(\""+ search + "\"):not(script)", element);
        }
        else {
                d = $(":contains(\""+ search + "\"):not(script)");
        }
    
        if (d.length == 1) {
                e.push(d[0]);
        }
        else {
            d.each(function () {
                var i, r = distinctNodes(search, this);
                if (r.length === 0) {
                    e.push(this);
                }
                else {
                    for (i = 0; i < r.length; ++i) {
                        e.push(r[i]);
                    }
                }
            });
        }
        $.each(e, function () {
            for (var i = 0; i < ef.length; ++i) {
                if (this === ef[i]) return;
            }
            ef.push(this);
        });
        return ef;
    }
    
    0 讨论(0)
  • 2020-11-22 08:02

    If you just want the node closest to the text you're searching for, you could use this:

    $('*:contains("my text"):last');
    

    This will even work if your HTML looks like this:

    <p> blah blah <strong>my <em>text</em></strong></p>
    

    Using the above selector will find the <strong> tag, since that's the last tag which contains that entire string.

    0 讨论(0)
  • 2020-11-22 08:03

    Take a look at highlight (jQuery plugin).

    0 讨论(0)
  • 2020-11-22 08:03

    Just adding to Tony Miller's answer as this got me 90% towards what I was looking for but still didn't work. Adding .length > 0; to the end of his code got my script working.

     $(function() {
        var foundin = $('*:contains("I am a simple string")').length > 0;
     });
    
    0 讨论(0)
  • 2020-11-22 08:06

    jQuery has the contains method. Here's a snippet for you:

    <script type="text/javascript">
    $(function() {
        var foundin = $('*:contains("I am a simple string")');
    });
    </script>
    

    The selector above selects any element that contains the target string. The foundin will be a jQuery object that contains any matched element. See the API information at: https://api.jquery.com/contains-selector/

    One thing to note with the '*' wildcard is that you'll get all elements, including your html an body elements, which you probably don't want. That's why most of the examples at jQuery and other places use $('div:contains("I am a simple string")')

    0 讨论(0)
  • 2020-11-22 08:06

    This will select just the leaf elements that contain "I am a simple string".

    $('*:contains("I am a simple string")').each(function(){
         if($(this).children().length < 1) 
              $(this).css("border","solid 2px red") });
    

    Paste the following into the address bar to test it.

    javascript: $('*:contains("I am a simple string")').each(function(){ if($(this).children().length < 1) $(this).css("border","solid 2px red") }); return false;
    

    If you want to grab just "I am a simple string". First wrap the text in an element like so.

    $('*:contains("I am a simple string")').each(function(){
         if($(this).children().length < 1) 
              $(this).html( 
                   $(this).text().replace(
                        /"I am a simple string"/
                        ,'<span containsStringImLookingFor="true">"I am a simple string"</span>' 
                   )  
               ) 
    });
    

    and then do this.

    $('*[containsStringImLookingFor]').css("border","solid 2px red");
    
    0 讨论(0)
提交回复
热议问题