ドキュメント

Selectors/has

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Selectors

:has(selector)

Matches elements which contain at least one element that matches the specified selector.
Arguments:
selectorSelector
A selector with which to filter by.
Examples:

Adds the class "test" to all divs that have a paragraph inside of them.

$("div:has(p)").addClass("test");

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  
  <script>
  $(document).ready(function(){
    $("div:has(p)").addClass("test");
  });
  </script>
  <style>
  .test{ border: 3px inset red; }
  </style>
</head>
<body>
  <div><p>Hello in a paragraph</p></div>
  <div>Hello again! (with no paragraph)</div>
</body>
</html>

NameType