Select only elements who directly contain text

RandallB

So I have something like this:

<div class="main">
  <div class="useless">
    text I don't want.    
  </div>
  <div class="useless">
    text I don't want.    
  </div>
  <div class="narrow">
    text I'm searching for
  </div>
  <div class="useless">
    text I don't want.    
  </div>
  <div class="useless">
    text I don't want.    
  </div>
</div>

Using jQuery's lovely :contains selector, I can search for a keyword, but it will return both the parent and the child.

I'd like it to only return elements who directly contain the word for which I'm searching.

Stackoverflow usefully suggested this link as a previous attempt, but it seems extremely clunky since it's crawling all dom nodes and requires a lot of unclear code.

Daniel Moses

This script will find all nodes that contain a specific text. It will also allow you to do any string tests on the text (regex etc).

function getNodesThatContain(text) {
    var textNodes = $(document).find(":not(iframe, script, style)")
      .contents().filter( 
          function() {
           return this.nodeType == 3 
             && this.textContent.indexOf(text) > -1;
    });
    return textNodes.parent();
};

console.log(getNodesThatContain("test"));

Here is a fiddle for testing: http://jsfiddle.net/85qEh/4/

PS - For increased speed use a different top level selector. For example if you only need inside #container then you would var textNodes = $('#container')...

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How do I select only people who share last names? SQL

来自分类Dev

在Postgres的jsonb_array_elements_text函数中使用SELECT子查询

来自分类Dev

how to select and sum directly preceded related rows

来自分类Dev

How to contain text box in parent div

来自分类Dev

Text Padding in Select Boxes

来自分类Dev

Select same elements matrix in R

来自分类Dev

Select specific elements from a vector

来自分类Dev

Angular $filter select property only

来自分类Dev

text-align: center placeholder text in select

来自分类Dev

VBA Debugger shows only 256 elements of a collection

来自分类Dev

Why is this only iterating over odd elements of HTMLCollection?

来自分类Dev

Iterating over only a subset of elements in set

来自分类Dev

SELECT FROM XML MULTIPLE ELEMENTS SQL

来自分类Dev

Ignore text inside XML elements when parsing text with Stanford CoreNLP

来自分类Dev

MYSQL SELECT only if rows are less than 5

来自分类Dev

Postgresql only select letters from a column

来自分类Dev

Mysql - Select from another table, operand should contain 1 column(s) error

来自分类Dev

How to restrict Shiny fileInput to text files only?

来自分类Dev

Customizing the text in a collection_select dropdown

来自分类Dev

join 2 mysql select based on text field

来自分类Dev

How do I highlight (select) text in Text widget with a button click?

来自分类Dev

I need a cell to be filled with a certain text when two other cells contain a certain value

来自分类Dev

Using nth-child to select an even, and then an odd number of elements

来自分类Dev

How to select all elements that don't have a class or id?

来自分类Dev

CSS select nested elements up to N levels deep

来自分类Dev

Text insertion cursor (caret) appears above other elements in mobile browser

来自分类Dev

Convert dumbquotes to smartquotes in text(), but ignore/retain child elements.

来自分类Dev

D3 - How to get width of previous elements in Text selection

来自分类Dev

find_elements_by_partial_link_text将找不到元素

Related 相关文章

  1. 1

    How do I select only people who share last names? SQL

  2. 2

    在Postgres的jsonb_array_elements_text函数中使用SELECT子查询

  3. 3

    how to select and sum directly preceded related rows

  4. 4

    How to contain text box in parent div

  5. 5

    Text Padding in Select Boxes

  6. 6

    Select same elements matrix in R

  7. 7

    Select specific elements from a vector

  8. 8

    Angular $filter select property only

  9. 9

    text-align: center placeholder text in select

  10. 10

    VBA Debugger shows only 256 elements of a collection

  11. 11

    Why is this only iterating over odd elements of HTMLCollection?

  12. 12

    Iterating over only a subset of elements in set

  13. 13

    SELECT FROM XML MULTIPLE ELEMENTS SQL

  14. 14

    Ignore text inside XML elements when parsing text with Stanford CoreNLP

  15. 15

    MYSQL SELECT only if rows are less than 5

  16. 16

    Postgresql only select letters from a column

  17. 17

    Mysql - Select from another table, operand should contain 1 column(s) error

  18. 18

    How to restrict Shiny fileInput to text files only?

  19. 19

    Customizing the text in a collection_select dropdown

  20. 20

    join 2 mysql select based on text field

  21. 21

    How do I highlight (select) text in Text widget with a button click?

  22. 22

    I need a cell to be filled with a certain text when two other cells contain a certain value

  23. 23

    Using nth-child to select an even, and then an odd number of elements

  24. 24

    How to select all elements that don't have a class or id?

  25. 25

    CSS select nested elements up to N levels deep

  26. 26

    Text insertion cursor (caret) appears above other elements in mobile browser

  27. 27

    Convert dumbquotes to smartquotes in text(), but ignore/retain child elements.

  28. 28

    D3 - How to get width of previous elements in Text selection

  29. 29

    find_elements_by_partial_link_text将找不到元素

热门标签

归档