在jquery选择器中包含变量

Tra*_*tic 2 variables jquery selector

我有一个快速的问题.我匹配LI元素的类名来打开和关闭具有匹配ID名称的div,如下所示:

$("ul#mainnav li").click(function() {
      $("#mainpages > div").fadeOut(200);
          var navClass = $(this).attr('class');
          var targeted='#'+navClass;
    $(targeted).fadeIn(200); // this is working
        $(targeted+'div:first').show(); // this is not working
});
Run Code Online (Sandbox Code Playgroud)

我想知道如何将变量"target"包含到选择器$(目标'div:first').show(); 这基本上是我想重置我的子页面以显示点击主导航项的第一个div,所以我想show()目标容器的第一个子div.我已经搜索但似乎无法使其正常工作.

谢谢你的任何建议!

Poi*_*nty 7

你需要在"div:first"中的"div"之前有一个空格:

$(targeted+' div:first').show();
Run Code Online (Sandbox Code Playgroud)

或者:

$(targeted).find('div:first').show();
Run Code Online (Sandbox Code Playgroud)