简体   繁体   English

选择不是当前元素/ JQuery父级的先前标题

[英]Select previous headings that are not parents of the current element / JQuery

I have the following markup 我有以下标记

<div id="section1">
  <h1>Titel 1</h1>
  <p>Some smart Text</p>
</div>
<div id="section2"> 
  <h2>Titel 2</h2>
  <p>Some smart Text2</p>
</div>
<div id="section3-1">
  <h3>Titel 3-1</h3>
</div>
<div id="section3-1content">
  <ul>
     <li id="section3-1content-el1">El1</li>
     <li id="section3-1content-el2">El2</li>
  </ul>
</div>
<div id="section3-2">
  <h3>Titel 3-2</h3>
</div>
<div id="section3-2content">
  <ul>
     <li id="section3-2content-el1">El1</li>
     <li id="section3-2content-el2">El2</li>
  </ul>
</div>

When I click on #section3-2content-el2 I want to get back 当我单击#section3-2content-el2时,我想返回

 <h3>Titel 3-2</3>

(the nearest h3), then (最近的h3),然后

 <h2>Titel 2</h2> 

and then 接着

 <h1>Titel 1</h1>.

The Problem with it is, that h3, h2 and h1 are not really parents from the clicked element. 问题在于,h3,h2和h1并不是所单击元素的真正父对象。 Is there any way to go back in the sourcecode and find the first h3, then the first h2 and then the first h1? 有什么办法可以返回源代码并找到第一个h3,然后找到第一个h2,然后找到第一个h1?

The Problem with: 问题在于:

$('#section3-2content-el1').click(function(){
  $('#section3-2content-el1').parents('h3');
  $('#section3-2content-el1').prevAll('h3');
  $('#section3-2content-el1').closest('h3');
});

is that they all travel up the dom and look if one is a h3. 是他们都沿着dom向上走,看一看是否是h3。 But the h3 is not a parent of the clicked element. 但是h3不是clicked元素的父级。

Demo Fiddle 演示小提琴

There are many options. 有很多选择。 Considering your html structure :last is the best option. 考虑到您的html结构:last是最好的选择。 Check the demo. 检查演示。

$(this).parents('body').find('h3:last')

If your heading tags were consistent, .prev() would have been another option. 如果您的标题标签一致,则.prev()将是另一个选择。

I would do this by using jQuery's .map() method to return an array (or, sort of an array, anyway) of class names. 我可以通过使用jQuery的.map()方法来返回类名的数组(或数组的排序.map()来做到这一点。 Then you can match the class name of the clicked element to the heading which shares its class name. 然后,您可以将clicked元素的类名称与共享其类名称的标题进行匹配。

First add some classes to the <h3> s and <li> s: 首先向<h3><li>添加一些类:

<div id="section1">
    <h1>Titel 1</h1>
    <p>Some smart Text</p>
</div>
<div id="section2"> 
    <h2>Titel 2</h2>
    <p>Some smart Text2</p>
</div>
<div id="section3-1">
    <h3 class="first">Titel 3-1</h3>
</div>
<div id="section3-1content">
    <ul>
        <li id="section3-1content-el1" class="first">El1</li>
        <li id="section3-1content-el2" class="first">El2</li>
    </ul>
</div>
<div id="section3-2">
    <h3 class="second">Titel 3-2</h3>
</div>
<div id="section3-2content">
    <ul>
        <li id="section3-2content-el1" class="second">El1</li>
        <li id="section3-2content-el2" class="second">El2</li>
    </ul>
</div>

And the JS/jQuery: 和JS / jQuery:

$(document).ready(function(){
    $('li').click(function(){
        var self = $(this);

        // Create an array of class names
        var cls = $('h3').map(function(){
            return $(this).attr('class');
        });

        // Loop through the array until the current iteration matches the class name of the clicked element
        for (var i = 0; i < cls.length; i++) {
            if (self.hasClass(cls[i])) {

                // Do stuff based on whether there is a match
                alert($('h3.' + cls[i]).attr('class'));
            }
        }
    });
});

FIDDLE: http://jsfiddle.net/KFS4E/ 内容: http : //jsfiddle.net/KFS4E/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM