Find text string using jQuery?

后端 未结 7 1398
暖寄归人
暖寄归人 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 08:14

    Normally jQuery selectors do not search within the "text nodes" in the DOM. However if you use the .contents() function, text nodes will be included, then you can use the nodeType property to filter only the text nodes, and the nodeValue property to search the text string.

        $('*', 'body')
            .andSelf()
            .contents()
            .filter(function(){
                return this.nodeType === 3;
            })
            .filter(function(){
                // Only match when contains 'simple string' anywhere in the text
                return this.nodeValue.indexOf('simple string') != -1;
            })
            .each(function(){
                // Do something with this.nodeValue
            });
    
    0 讨论(0)
提交回复
热议问题