javascript - 仅向父 li 添加类

标签 javascript jquery html

我有这样的结构:

<li id="artworks"><a href="#">ARTWORK</a>
                <ul class="submenu">
                    <img src="../img/submenu.png" alt="submenu" width="62" height="1" />
                    <li id="sweetlife"><a href="#">Sweet Life</a></li>
                    <li><a href="#">Pleasure</a></li>
                    <li><a href="#">Bienal de la habana</a></li>
                    <li><a href="#">Estudios de craneos</a></li>width="62" height="1" />
                </ul>
            </li>

我正在使用这个脚本:

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

问题是我只想将 active 类添加到“li id="artworks"”元素,但是使用脚本它会添加到属于#artworks 的所有“li”。

我怎样才能只指向父 li?

最佳答案

尝试

$('#artworks > a').addClass('active')

关于javascript - 仅向父 li 添加类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12857890/

相关文章:

javascript - 在 qtip2 中使用 themeroller 主题

javascript - angularjs 测试指令修改用户输入

jquery切换子菜单但如果子菜单页面处于事件状态则保持子菜单打开

php - jquery 窗口位置在 php echo 中不起作用?

javascript - 当用户向下滚动时如何使向下箭头消失?

javascript - 尝试在回调函数中呈现 JSX 后抛出语法错误 'Unexpected token, expected }'

c# - 我可以在 javascript 中使用 resolveurl

javascript - 刷新数据源自动完成 jQuery

html - 自动 CSS 高度

javascript - 有没有 javascript ssh 客户端?