JQuery 选择器问题:如何选择当前列表项的子项

发布于 2024-09-19 14:47:16 字数 854 浏览 1 评论 0原文

我确信这个标题令人困惑。这是 WordPress 的事情。

所以,我有一个页面列表,结构如下所示:

<div id="menu">
  <ul> <!--List of pages-->
    <li class="page-item page-id">
      <a href="Page url"> Ham Man </a>
        <ul class="children">
             <li class="page-item page-id">
             <a href="Page url"> Page 1</a></li>
              <li class="page-item page-id">
              <a href="Page url"> Page 2</a></li>
        </ul>
     </li>
   </ul>
 </div>

我的问题是这样的 - 有多个标题,例如“Ham Man”。因此,我需要使用一点 jQuery,以便当有人单击某个标题时,它会显示该标题的页面。很容易。但当我有多个标题时,这就变得很棘手。

我需要说的是“当有人单击菜单列中的 li 元素时,仅显示该 LI 元素的所有子元素(类“children”)”。

这一定很简单,但我做不到。

可靠的 B 计划只是让 Wordpress 将“Parent”类放入“Parent”元素中,但这似乎是谋杀的秘诀。

想法?提前致谢!

I'm sure that title is confusing. This is a Wordpress thing.

So, I have a list of pages and the structure looks like this:

<div id="menu">
  <ul> <!--List of pages-->
    <li class="page-item page-id">
      <a href="Page url"> Ham Man </a>
        <ul class="children">
             <li class="page-item page-id">
             <a href="Page url"> Page 1</a></li>
              <li class="page-item page-id">
              <a href="Page url"> Page 2</a></li>
        </ul>
     </li>
   </ul>
 </div>

My problem is this- There are MULTIPLE titles like 'Ham Man'. So what I need to is work a bit of jQuery so when someone clicks on a title, it displays the PAGES for that title. Easy enough. But when I have multiple titles, this gets tricky.

WHat I need to say is "WHEN someone clicks on a li element in the menu column, display all of the CHILD ELEMENTS (class 'children') JUST for that LI element".

It's gotta be simple, but I can't work it.

A solid Plan B is just to have Wordpress drop a class of 'Parent' into the 'Parent' elements, but that seems like a recipe for MURDER.

Thoughts? Thanks ahead of time!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朮生 2024-09-26 14:48:29

应该是:

$('#menu > ul > li').bind('click', function(){
    var elems = $(this).find('.children').children();

     // elemens contains all child nodes from ul.children
});

should be:

$('#menu > ul > li').bind('click', function(){
    var elems = $(this).find('.children').children();

     // elemens contains all child nodes from ul.children
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文