jQuery: find element by text

后端 未结 7 2085
攒了一身酷
攒了一身酷 2020-11-22 03:31

Can anyone tell me if it\'s possible to find an element based on its content rather than by an id or class?

I am attempting to find

7条回答
  •  遇见更好的自我
    2020-11-22 04:15

    The following jQuery selects div nodes that contain text but have no children, which are the leaf nodes of the DOM tree.

    $('div:contains("test"):not(:has(*))').css('background-color', 'red');
    div1
    This is a test, nested in div1
    Nested in div1
    div2 test
    This is another test, nested in div2
    Nested in div2
    div3

提交回复
热议问题