Find parent element of string in Javascript

后端 未结 3 1790
醉话见心
醉话见心 2021-01-28 17:05

I\'m attempting to find a specific string in a document which potentially can have the text split by other tags (i.e. "< p > This is an < span > example < /s

3条回答
  •  鱼传尺愫
    2021-01-28 18:10

    This will get you started. You can use :contains() selector for finding string in html.

    function getStringParent(str) { 
      return $("p:contains('"+ str +"')");
    }
    var parent = getStringParent('This is an  example');
    console.log('found ' + parent.length + ' items' + '\n');
    console.log(parent);
    
    

    This is an example

提交回复
热议问题