:has() Selector
has selector
version added: 1.1.4jQuery(':has(selector)')
描述: 選擇含有選擇器所匹配的至少一個(gè)元素的元素。
表達(dá)式 $('div:has(p)') 匹配一個(gè) <div>如果<p>在其后代中存在的任何地方,不僅是一種直接的子元素。
Example:
Adds the class "test" to all divs that have a paragraph inside of them.
<!DOCTYPE html>
<html>
<head>
<style>
.test{ border: 3px inset red; }
</style>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<div><p>Hello in a paragraph</p></div>
<div>Hello again! (with no paragraph)</div>
<script>$("div:has(p)").addClass("test");</script>
</body>
</html>