jQuery select elements that contain specific child elements

Description

jQuery select elements that contain specific child elements

View in separate window

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Selecting Elements that Contain Specific Child Elements in jQuery</title>
<style>
    .highlight{/*from  www.  jav  a  2 s  . c  o m*/
        background: yellow;
    }
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function(){
    $("ul li").has("ul").addClass("highlight");
});
</script>
</head>
<body>
    <ul>
        <li>Section 1</li>
        <li>Section 2</li>
        <li>
            <ul>
                <li>Section 2.1</li>
                <li>Section 2.2</li>
                <li>Section 2.3</li>
            </ul>
        </li>
        <li>Section 4</li>
    </ul>
</body>
</html>



PreviousNext

Related