jQuery

[jQuery] jQuery / Selector /: contains ()-Selector that selects elements that contain a specific string

OOQ 2022. 7. 28. 15:33
728x90
SMALL

#jQuery

:contains()

: contains () is a selector that selects elements that contain a particular string.

grammar

$( ':contains(text)' )

Keep in mind that it is case sensitive when choosing whether to include strings. for example

$( 'p:contains("ab")' )

Select the p element that contains the ab string.

 

example

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>jQuery</title>
    <script src="//code.jquery.com/jquery-1.12.4.min.js"></script>
    <script>
      $( document ).ready( function() {
        $( 'p:contains("jb")' ).css( 'color', 'red' );
      } );
    </script>
  </head>
  <body>
    <p>Lorem ipsum dolor sit amet.</p>
    <p>Aenean nec mollis jb nulla.</p>
    <p>Phasellus JB lacinia tempus mauris eu laoreet.</p>
    <p>Proin gravida velit dictum jbdui consequat.</p>
  </body>
</html>

Select the p element that contains jb to make the text red. The first paragraph has no jb and the third paragraph has jb, but it is not selected in uppercase.

 

728x90
LIST