Javascript examples for jQuery Method and Property:filter
Filter selected elements by criteria function
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("p").filter(function(){return $("span", this).length == 1;}).css("background-color", "red"); });//from w w w . jav a 2 s. com </script> </head> <body> <div> <p>A p element <span>with one span element.</span></p> <p>A p element <span>with</span> two <span>span elements.</span></p> <p>A p element <span>with one span element.</span></p> <p>A p element <span>with</span> two <span>span elements.</span></p> <p>A p element with no span element.</p> </div> <div> <p>A p element <span>with one span element.</span></p> <p>A p element <span>with</span> two <span>span elements.</span></p> <p>A p element <span>with one span element.</span></p> <p>A p element <span>with</span> two <span>span elements.</span></p> <p>A p element with no span element.</p> </div> </body> </html>