jquery 查找上一个元素的元素

发布于 2024-12-08 18:16:29 字数 2330 浏览 4 评论 0原文

好的。我的 html 设置如下。

<div id="wrap">
   <div class="left"></div>
   <div class="right">
      <a href="#">link</a>
   </div>
   <div class="listings" style="display:none">
      <ul>
         <li>Listing 1</li>
         <li>Listing 2</li>
         <li>Listing 3</li>
      </ul>
   </div>
</div>

我想做的是,当单击“右侧”中的链接时,它将找到列表 div,然后显示它,问题是,我需要在显示它之前知道 UL 容器的高度,以便我可以调整大小并显示因此。

另一个问题是,内容是动态创建的,因此页面上可以显示多个左/右/列表组合。我不处理产品的那部分,我只是开发前端 UI 来处理发布的内容。所以我陷入了僵局,因为我已经尝试了从兄弟姐妹到父母的所有我能想到的方法,却发现也许我只是解决了错误。

这是一个与上面相同的例子,但是如果有多个的话,它会是什么样子实例(我认为这是我的问题的一部分)..

<div id="wrap">
       <div class="left"></div>
       <div class="right">
          <a href="#">link</a>
       </div>
       <div class="listings" style="display:none">
          <ul>
             <li>Listing 1</li>
             <li>Listing 2</li>
             <li>Listing 3</li>
          </ul>
       <div>
       <div class="left"></div>
       <div class="right">
          <a href="#">link</a>
       </div>
       <div class="listings" style="display:none">
          <ul>
             <li>Listing 1</li>
             <li>Listing 2</li>
             <li>Listing 3</li>
          </ul>
       <div>
       <div class="left"></div>
       <div class="right">
          <a href="#">link</a>
       </div>
       <div class="listings" style="display:none">
          <ul>
             <li>Listing 1</li>
             <li>Listing 2</li>
             <li>Listing 3</li>
          </ul>
       <div>
       <div class="left"></div>
       <div class="right">
          <a href="#">link</a>
       </div>
       <div class="listings" style="display:none">
          <ul>
             <li>Listing 1</li>
             <li>Listing 2</li>
             <li>Listing 3</li>
          </ul>
       <div>
    </div>

Ok. I have my html setup like.

<div id="wrap">
   <div class="left"></div>
   <div class="right">
      <a href="#">link</a>
   </div>
   <div class="listings" style="display:none">
      <ul>
         <li>Listing 1</li>
         <li>Listing 2</li>
         <li>Listing 3</li>
      </ul>
   </div>
</div>

What I am trying to do is when the link in "right" is clicked it will find the listings div and then show it, problem is, I need to know the height of the UL container prior to showing it so I can resize and show accordingly.

The other issue is, the content is dynamicly created so it can have multiple left/right/listings combinations displayed on the page. I don't handle that portion of the product I just develop the front end UI to work with the stuff put out. So I'm at an impass as I have tried everything I can think of from siblings to parents to find maybe I am just tackling it wrong..

Here is an example of the same as above but what it can look like with more than one instance (which is part of my issue I think)..

<div id="wrap">
       <div class="left"></div>
       <div class="right">
          <a href="#">link</a>
       </div>
       <div class="listings" style="display:none">
          <ul>
             <li>Listing 1</li>
             <li>Listing 2</li>
             <li>Listing 3</li>
          </ul>
       <div>
       <div class="left"></div>
       <div class="right">
          <a href="#">link</a>
       </div>
       <div class="listings" style="display:none">
          <ul>
             <li>Listing 1</li>
             <li>Listing 2</li>
             <li>Listing 3</li>
          </ul>
       <div>
       <div class="left"></div>
       <div class="right">
          <a href="#">link</a>
       </div>
       <div class="listings" style="display:none">
          <ul>
             <li>Listing 1</li>
             <li>Listing 2</li>
             <li>Listing 3</li>
          </ul>
       <div>
       <div class="left"></div>
       <div class="right">
          <a href="#">link</a>
       </div>
       <div class="listings" style="display:none">
          <ul>
             <li>Listing 1</li>
             <li>Listing 2</li>
             <li>Listing 3</li>
          </ul>
       <div>
    </div>

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

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

发布评论

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

评论(3

放我走吧 2024-12-15 18:16:29

这是你想要的吗?:

$(".right a").click(function( e ) {
    $(this) //a-element
        .parent() // div.right
            .next() // div.listings
            .toggle(); //Hide and show depending on what it was
    e.preventDefault();
});

如果你愿意,你可以删除注释并将其写在一行上。

如果它是通过 AJAX 动态加载的或者未在 DOM 上加载,您可以使用实时事件:

$(".right a").click("click", function( e ) {
    $(this) //a-element
        .parent() // div.right
            .next() // div.listings
            .toggle(); //Hide and show depending on what it was
    e.preventDefault();
});

is this what you want?:

$(".right a").click(function( e ) {
    $(this) //a-element
        .parent() // div.right
            .next() // div.listings
            .toggle(); //Hide and show depending on what it was
    e.preventDefault();
});

You can remove the comments and write it on one line if you want.

if its dynamic loaded with AJAX or not loaded on the DOM you can use live event:

$(".right a").click("click", function( e ) {
    $(this) //a-element
        .parent() // div.right
            .next() // div.listings
            .toggle(); //Hide and show depending on what it was
    e.preventDefault();
});
漫雪独思 2024-12-15 18:16:29

您只需上升一级(parent),移至下一个元素(next),然后显示它:

$('.right a').click(function() {
    $(this).parent().next().show();
});

演示:http://jsfiddle.net/ambigously/jRVxe/

我假设你的 HTML 有点混乱,我想很多的

应该是

<div id="wrap">
    <div class="left"></div>
    <div class="right">
        <a href="#">link</a>
    </div>
    <div class="listings" style="display:none">
        <ul>
            <li>Listing 1</li>
            <li>Listing 2</li>
            <li>Listing 3</li>
        </ul>
    </div>
    <div class="left"></div>
    <div class="right">
        <a href="#">link</a>
    </div>
    <div class="listings" style="display:none">
        <ul>
            <li>Listing 1</li>
            <li>Listing 2</li>
            <li>Listing 3</li>
        </ul>
    </div>
    <div class="left"></div>
    <div class="right">
        <a href="#">link</a>
    </div>
    <div class="listings" style="display:none">
        <ul>
            <li>Listing 1</li>
            <li>Listing 2</li>
            <li>Listing 3</li>
        </ul>
    </div>
    <div class="left"></div>
    <div class="right">
        <a href="#">link</a>
    </div>
    <div class="listings" style="display:none">
        <ul>
            <li>Listing 1</li>
            <li>Listing 2</li>
            <li>Listing 3</li>
        </ul>
    </div>
</div>

You just need to go up one level (parent), move to the next element (next), and show it:

$('.right a').click(function() {
    $(this).parent().next().show();
});

Demo: http://jsfiddle.net/ambiguous/jRVxe/

I'm assuming that you're HTML was a little messed up, I think a lot of the <div> should have been </div>:

<div id="wrap">
    <div class="left"></div>
    <div class="right">
        <a href="#">link</a>
    </div>
    <div class="listings" style="display:none">
        <ul>
            <li>Listing 1</li>
            <li>Listing 2</li>
            <li>Listing 3</li>
        </ul>
    </div>
    <div class="left"></div>
    <div class="right">
        <a href="#">link</a>
    </div>
    <div class="listings" style="display:none">
        <ul>
            <li>Listing 1</li>
            <li>Listing 2</li>
            <li>Listing 3</li>
        </ul>
    </div>
    <div class="left"></div>
    <div class="right">
        <a href="#">link</a>
    </div>
    <div class="listings" style="display:none">
        <ul>
            <li>Listing 1</li>
            <li>Listing 2</li>
            <li>Listing 3</li>
        </ul>
    </div>
    <div class="left"></div>
    <div class="right">
        <a href="#">link</a>
    </div>
    <div class="listings" style="display:none">
        <ul>
            <li>Listing 1</li>
            <li>Listing 2</li>
            <li>Listing 3</li>
        </ul>
    </div>
</div>
第几種人 2024-12-15 18:16:29

首先,您必须为其保留一些映射,例如保留用于列出 div 的 ID,并在单击包含该 id 的链接时调用函数。

<前><代码>>

>

>

否则你会很困惑

First of all you must keep some mapping for it for say keep an ID for listing divs and and invoke a function on click of link that contains the id.

>  <div class="left"></div>
>        <div class="right">
>           <a href="#" onclick="someFunction('id1');">link</a>
>        </div>
>        <div id="id1" class="listings" style="display:none">
>           <ul>
>              <li>Listing 1</li>
>              <li>Listing 2</li>
>              <li>Listing 3</li>
>           </ul>
>        <div>

else it will be very confusing for you

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