简体   繁体   English

jQuery .prev()奇怪的行为

[英]jquery .prev() strange behavior

I have a very simple list of LI's and they are all on the same level (nothing nested) I want to find the previous element of any element who's class is level1. 我有一个非常简单的LI列表,它们都在同一级别上(没有嵌套),我想找到任何类别为level1的元素的前一个元素。 For some reason prev() is failing on items when the 'level1' element is more than one sibling away. 由于某些原因,当'level1'元素相距一个以上时,prev()在项目上失败。 I don't want to use prevAll() because I only want the closest li that comes before my element and has a class of level1. 我不想使用prevAll(),因为我只想要在我的元素之前并且具有class1类的最接近的li。

<ul id="main-list">
    <li class="level1" data-id="1" data-parent="none" id="recordsArray_1">Overview</li>
    <li class="level2" data-id="4" data-parent="Overview" id="recordsArray_4">Benefits</li>
    <li class="level2" data-id="2" data-parent="undefined" id="recordsArray_2">Core Concepts</li>
    <li class="level2" data-id="3" data-parent="undefined" id="recordsArray_3">Access</li>
    <li class="level3" data-id="5" data-parent="Access" id="recordsArray_5">Onboarding</li>
    <li class="level1" data-id="12" data-parent="none" id="recordsArray_12">test top</li>
    <li class="level2" data-id="10" data-parent="test top" id="recordsArray_10">test 1</li>
    <li class="level2" data-id="14" data-parent="undefined" id="recordsArray_14">New Access</li>
    <li class="level3" data-id="13" data-parent="New Access" id="recordsArray_13">test 2</li>
    <li class="level1" data-id="6" data-parent="none" id="recordsArray_6">Underview</li>
    <li class="level2" data-id="7" data-parent="Underview" id="recordsArray_7">coolpage1</li>
    <li class="level3" data-id="9" data-parent="coolpage1" id="recordsArray_9">page level 3</li>
</ul>

<script>
var publishList = $('#main-list li');

$.each(publishList, function () {
    var thisClass = $(this).attr('class');
    var parentID = $(this).attr('data-parent');
    var recordID = $(this).attr('data-id');

    if (thisClass == 'level2') {
        var thisParent = $(this).prev('.level1').html();
        console.log(this);    
    }
}); 
</script>

console output: 控制台输出:

Overview
undefined
undefined
test top
undefined
Underview

What am I doing wrong? 我究竟做错了什么? I expected prev() to return overview for core concepts, access and New Access but they are coming back undefined. 我希望prev()返回有关核心概念,访问权限和新访问权限的概述,但它们回来后仍未定义。 Is there a flaw in my logic? 我的逻辑有缺陷吗?

Try a combination of .prevAll() and .first() 尝试的组合.prevAll().first()

var thisParent = $(this).prevAll('.level1').first().html();

This will make sure you always get the closest possible sibling no matter the distance. 这将确保无论距离多远,您始终会获得最接近的同级兄弟。

Try this code 试试这个代码

var publishList = $('#main-list li');

$.each(publishList, function () {
    var $this = $(this),
        thisClass = $this.attr('class'),
        parentID = $this.attr('data-parent'),
        recordID = $this.attr('data-id');

    if (thisClass == 'level2') {
        var thisParent = $this.prevAll('.level1').first().html();
        $('#output').append($this.prevAll('.level1').first().html()+'<br />');
    }

});

Check Fiddle 检查小提琴

.prev() doesn't provide the functionality you want. .prev()没有提供您想要的功能。 The functionality you're looking for is in .prevAll() 您正在寻找的功能在.prevAll()

Once you have a set of values from .prevAll() you can use something like .first() to get what you need (as has been mentioned) or identify the closest element by id (ie .split() a number-based id or some similar method). 一旦你有一组值的.prevAll()你可以使用类似.first()来获得你所需要的(如已经提到)或ID(识别最接近的元素,即.split()一个基于数字的ID或类似方法)。 However you decide to go about it, .prev() won't get you what you want. 但是,您决定执行此操作, .prev()不会为您提供所需的内容。

只需更新您的if语句

    if (thisClass == 'level2' && $(this).prev('.level1').length) {

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

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