为什么这个jQuery脚本不会突出显示我列表中的第一个孩子

How come this jQuery script will not highlight the first child in my list?

本文关键字:列表 显示 孩子 第一个 jQuery 脚本 为什么      更新时间:2023-09-26
<html>
<head>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
    <style>
    .emphasis { font-weight: bold; } 
    </style>
    <script>
    $(function() {
        $('li:first-child').addClass('emphasis');
    });
    </script>
    
</head>
<body>
<ul>
    <li>hello</li>
    <li>hello2</li>
    <li>hello3</li>
</ul>
</body>
</html>

我正在遵循一个教程:http://tutsplus.com/lesson/not-so-fast-jquery/

这段代码正是他在结尾处拥有的代码,但它对我不起作用。有什么原因吗?

代码工作正常。尝试清除浏览器缓存或在其他浏览器中尝试。

尝试使用$('li:first')