0

我试图选择li列表第二级的所有元素,向它们添加特殊类。我做了几次尝试都没有成功。所以li我瞄准的元素是喜剧、悲剧和历史下面的水平。

<script type="text/javascript">
    $(document).ready(function () {
        $('li').next().addClass('special');
    });
</script>

<style type="text/css">
    .special {
        font-style: italic;
    }
</style>

<body>
    <div id="container">
        <h2>Selected Shakespeare Plays</h2>
        <ul id="selected-plays" class="clear-after">
            <li>Comedies
                <ul>
                    <li>
                        <a href="/asyoulikeit/">As You Like It</a>
                    </li>
                    <li>All's Well That Ends Well</li>
                    <li>A Midsummer Night's Dream</li>
                    <li>Twelfth Night</li>
                </ul>
            </li>
            <li>Tragedies
                <ul>
                    <li>
                        <a href="hamlet.pdf">Hamlet</a>
                    </li>
                    <li>Macbeth</li>
                    <li>Romeo and Juliet</li>
                </ul>
            </li>
            <li>Histories
                <ul>
                    <li>Henry IV (
                        <a href="mailto:henryiv@king.co.uk">email</a>)
                        <ul>
                            <li>Part I</li>
                            <li>Part II</li>
                        </ul>
                    </li>
                    <li>
                        <a href="http://www.shakespeare.co.uk/henryv.htm">Henry V</a>
                    </li>
                    <li>Richard II</li>
                </ul>
            </li>
        </ul>
</body>
4

2 回答 2

5

试试这个:

<script type="text/javascript">
        $(document).ready(function(){
            $('#selected-plays > li > ul > li').addClass('special');
        });

</script>
于 2012-07-03T13:45:45.313 回答
0

或者

$('#selected-plays li').find('li').addClass('special');

有很多方法可以在集合中找到孩子

于 2012-07-03T13:49:58.333 回答