Find text string using jQuery?

后端 未结 7 1486
暖寄归人
暖寄归人 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;
    }
    

提交回复
热议问题