如何更改 $(this) 子级的 css

发布于 2024-09-11 08:25:19 字数 2653 浏览 2 评论 0 原文

我有一个搜索结果页面,每个结果都有一个图标,用于将每个结果添加到一个组中(在页面上以隐藏的 UL 元素列出:display:none;)。

现在它可以工作...但是当我单击一个结果列表的图标时...UL 出现在每个结果下方。

我需要它只显示我单击图标的结果列表...不是所有列表...

目前的加载代码是这样的:

$("a.quicklist, a[href=#newgrp]").toggle(
    function(){
        $("ul.quicklist_menu").css('display','block');  
        $(this).addClass("current");
    },
    function(){
        $("ul.quicklist_menu").css('display','none');
        $(this).removeClass("current");
});

我尝试将其更改为这样:

$("a.quicklist, a[href=#newgrp]").toggle(
    function(){
        $(this).children("ul.quicklist_menu").css('display','block');   
        $(this).addClass("current");
    },
    function(){
        $(this).children("ul.quicklist_menu").css('display','none');    
        $(this).removeClass("current");
});

但是,是的..图标突出显示为“当前”或活动,但 UL 未出现...

任何帮助将不胜感激。

面向对象。对不起!这是 HTML:

<li class='vcard'>
    <a href='profile.php?id=1288'><img class='user-photo' src='profile_images/sq_avatar.png' alt='Paul Dietzel' /></a>
    <div class='user-info'>
        <a class='fn url name' href='profile.php?id=1288'>Paul Dietzel</a>
        <ul class='attributes'>
            <li class='adr'><span class='locality'>Los Angeles</span>, <abbr class='region' title='California'>CA</abbr></li>
        </ul>
    </div>
    <ul class='options'>
        <li><a class='view-profile' href='profile.php?id=1288'>View profile</a></li>
        <li><abbr title="Add/Remove Favorite"><button class="favorite ICON" id="1288">Remove Favorite</button></abbr></li>
        <li><a class='add-to-group' href='#newgrp'>Add to group</a></li>
        <ul class="quicklist_menu"><li><a href='#addgrp' id='1147' rel='1988'>Test 3</a></li>
            <li><a href='#addgrp' id='1147' rel='1989'>Test 4</a></li>
            <li><a href='#addgrp' id='1147' rel='1990'>Test 5</a></li>
            <li><a href='#addgrp' id='1147' rel='1991'>Test 7</a></li>
            <li><a href='#addgrp' id='1147' rel='1129'>Lou Herthum Acting Class</a></li>
            <li><a href='#addgrp' id='1147' rel='1967'>test group</a></li>
            <li class="mgrp"><a href="groups.php#newgrp">Manage Groups &raquo;</a></li>
        </ul>
    </ul>
</li>

I have a page of search results, each of which has an icon to add each result to a group (listed on the page in a hidden UL element: display:none;).

Right now it works... but when I click on the icon for one result listing... the UL appears under every single result.

I need it to only show up for the result listing that I am clicking the icon on... not all the listings...

Currently the onload code is this:

$("a.quicklist, a[href=#newgrp]").toggle(
    function(){
        $("ul.quicklist_menu").css('display','block');  
        $(this).addClass("current");
    },
    function(){
        $("ul.quicklist_menu").css('display','none');
        $(this).removeClass("current");
});

I tried to change it to this:

$("a.quicklist, a[href=#newgrp]").toggle(
    function(){
        $(this).children("ul.quicklist_menu").css('display','block');   
        $(this).addClass("current");
    },
    function(){
        $(this).children("ul.quicklist_menu").css('display','none');    
        $(this).removeClass("current");
});

But, yeh.. the icon is highlighted as "current" or active, but the UL does not appear...

Any help would be greatly appreciated.

OOPs. Sorry! Here is the HTML:

<li class='vcard'>
    <a href='profile.php?id=1288'><img class='user-photo' src='profile_images/sq_avatar.png' alt='Paul Dietzel' /></a>
    <div class='user-info'>
        <a class='fn url name' href='profile.php?id=1288'>Paul Dietzel</a>
        <ul class='attributes'>
            <li class='adr'><span class='locality'>Los Angeles</span>, <abbr class='region' title='California'>CA</abbr></li>
        </ul>
    </div>
    <ul class='options'>
        <li><a class='view-profile' href='profile.php?id=1288'>View profile</a></li>
        <li><abbr title="Add/Remove Favorite"><button class="favorite ICON" id="1288">Remove Favorite</button></abbr></li>
        <li><a class='add-to-group' href='#newgrp'>Add to group</a></li>
        <ul class="quicklist_menu"><li><a href='#addgrp' id='1147' rel='1988'>Test 3</a></li>
            <li><a href='#addgrp' id='1147' rel='1989'>Test 4</a></li>
            <li><a href='#addgrp' id='1147' rel='1990'>Test 5</a></li>
            <li><a href='#addgrp' id='1147' rel='1991'>Test 7</a></li>
            <li><a href='#addgrp' id='1147' rel='1129'>Lou Herthum Acting Class</a></li>
            <li><a href='#addgrp' id='1147' rel='1967'>test group</a></li>
            <li class="mgrp"><a href="groups.php#newgrp">Manage Groups »</a></li>
        </ul>
    </ul>
</li>

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

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

发布评论

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

评论(2

荆棘i 2024-09-18 08:25:19

您需要通过将

    移动为锚点的同级来更正标记,而不是在

  • 之外。 但在内部(

      不能是

        的子级,它是无效的 HTML)。像这样:
<ul class='options'>
  <li><a class='view-profile' href='profile.php?id=1288'>View profile</a></li>
  <li><abbr title="Add/Remove Favorite"><button class="favorite ICON" id="1288">Remove Favorite</button></abbr></li>
  <li><a class='add-to-group' href='#newgrp'>Add to group</a>
    <ul class="quicklist_menu">
      <li><a href='#addgrp' id='1147' rel='1988'>Test 3</a></li>
      <li><a href='#addgrp' id='1147' rel='1989'>Test 4</a></li>
      <li><a href='#addgrp' id='1147' rel='1990'>Test 5</a></li>
      <li><a href='#addgrp' id='1147' rel='1991'>Test 7</a></li>
      <li><a href='#addgrp' id='1147' rel='1129'>Lou Herthum Acting Class</a></li>
      <li><a href='#addgrp' id='1147' rel='1967'>test group</a></li>
      <li class="mgrp"><a href="groups.php#newgrp">Manage Groups »</a></li>
    </ul>
  </li>
</ul>

然后你可以像这样使用 .next()

$("a.quicklist, a[href=#newgrp]").toggle(
    function(){
        $(this).addClass("current").next("ul.quicklist_menu").show();  
    },
    function(){
        $(this).removeClass("current").next("ul.quicklist_menu").hide();
});

这使用 .show().hide() 快捷方式也可以应用显示。或者,您可以使用 .click() 来切换使用 .toggleClass().toggle(),如下所示:

$("a.quicklist, a[href=#newgrp]").click(function() {
  $(this).toggleClass("current").next("ul.quicklist_menu").toggle();
});

You need to correct your markup by moving that <ul> to be a sibling of the anchor, not outside the <li></li> but inside (<ul> can't be a child of a <ul>, it's invalid HTML). Like this:

<ul class='options'>
  <li><a class='view-profile' href='profile.php?id=1288'>View profile</a></li>
  <li><abbr title="Add/Remove Favorite"><button class="favorite ICON" id="1288">Remove Favorite</button></abbr></li>
  <li><a class='add-to-group' href='#newgrp'>Add to group</a>
    <ul class="quicklist_menu">
      <li><a href='#addgrp' id='1147' rel='1988'>Test 3</a></li>
      <li><a href='#addgrp' id='1147' rel='1989'>Test 4</a></li>
      <li><a href='#addgrp' id='1147' rel='1990'>Test 5</a></li>
      <li><a href='#addgrp' id='1147' rel='1991'>Test 7</a></li>
      <li><a href='#addgrp' id='1147' rel='1129'>Lou Herthum Acting Class</a></li>
      <li><a href='#addgrp' id='1147' rel='1967'>test group</a></li>
      <li class="mgrp"><a href="groups.php#newgrp">Manage Groups »</a></li>
    </ul>
  </li>
</ul>

Then you can use .next() like this:

$("a.quicklist, a[href=#newgrp]").toggle(
    function(){
        $(this).addClass("current").next("ul.quicklist_menu").show();  
    },
    function(){
        $(this).removeClass("current").next("ul.quicklist_menu").hide();
});

This uses the .show() and .hide() shortcuts to apply the display as well. Alternatively, you can use .click() to toggle using .toggleClass() and .toggle(), like this:

$("a.quicklist, a[href=#newgrp]").click(function() {
  $(this).toggleClass("current").next("ul.quicklist_menu").toggle();
});
屋檐 2024-09-18 08:25:19

使用 .closest().next()

$("a.quicklist, a[href=#newgrp]").toggle(
    function(){
        $(this).closest('li').next("ul.quicklist_menu").css('display','block');   
        $(this).addClass("current");
    },
    function(){
        $(this).closest('li').next("ul.quicklist_menu").css('display','none');    
        $(this).removeClass("current");
});

或者您也可以尝试这样,

$("a.quicklist, a[href=#newgrp]").toggle(
    function(){
        $(this).addClass("current")
            .closest('ul.options')
            .children("ul.quicklist_menu")
            .css('display','block');
    },
    function(){
        $(this).removeClass("current")
            .closest('ul.options')
            .children("ul.quicklist_menu")
            .css('display','none');    
});

use .closest() and .next()

$("a.quicklist, a[href=#newgrp]").toggle(
    function(){
        $(this).closest('li').next("ul.quicklist_menu").css('display','block');   
        $(this).addClass("current");
    },
    function(){
        $(this).closest('li').next("ul.quicklist_menu").css('display','none');    
        $(this).removeClass("current");
});

or you may also try like this,

$("a.quicklist, a[href=#newgrp]").toggle(
    function(){
        $(this).addClass("current")
            .closest('ul.options')
            .children("ul.quicklist_menu")
            .css('display','block');
    },
    function(){
        $(this).removeClass("current")
            .closest('ul.options')
            .children("ul.quicklist_menu")
            .css('display','none');    
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文