javascript - prevAll() 在更复杂的 DOM 中不起作用

标签 javascript jquery html

单击按钮后,我尝试在 DOM 中进一步选择单个同级。我只想向上找到最接近的同级(在生产中,按钮之间有更多的 HTML,因此更有必要以这种方式将其拆分)。我使用 prevAll(selector:first) 来执行此操作,当我使用非常简单的版本 ( here ) 时,它似乎工作正常。但是当我以更类似于我的实际环境的方式执行此操作时, prevAll() 似乎无法找到同级元素 div.googleMapsContainer:

这是我的 HTML:

  <div style="display: none;" class="googleMapsContainer" id="gm1">
        <div class="gmap" style="max-width: 80%;
            max-height: 400px;
            background-color: grey;" class="map">
        </div>
        This Text Should be visible after click
 </div>

 <div class="tableProcessingToolBarContainer">
      <div class="container-fluid tableProcessingTools" >
        <div class="row-fluid">
         <div class="appSliderContent tptSliderContent container-fluid ">
                <button class="gmapInit glassyButton">View data in Google Maps</button>
         </div>
        </div>
      </div>
    </div>

 <div style="display: none;" class="googleMapsContainer" id="gm2">
        <div class="gmap" style="max-width: 80%;
            max-height: 400px;
            background-color: grey;" class="map">
        </div>
        This Text Should be visible after click
 </div>

    <br>

    <div class="tableProcessingToolBarContainer">
      <div class="container-fluid tableProcessingTools" >
        <div class="row-fluid">
         <div class="appSliderContent tptSliderContent container-fluid ">
                <button class="gmapInit glassyButton">View data in Google Maps</button>
         </div>
        </div>
      </div>
    </div>

这是我的 JavaScript:

$(document).on('click', '.gmapInit', function() {
    console.log("here is this: ");
    console.log($(this));
    var closeThing = $(this).prevAll("div.googleMapsContainer:first");
    console.log("here is the closest: ");
    console.log(closeThing);
    closeThing.attr("style", "");
});

控制台显示 closeThing 没有任何结果,因为 prevAll 没有成功。这是一个用于演示的 JsFiddle:https://jsfiddle.net/NateH06/twk9mtjg/

最佳答案

这个对我有用,我刚刚修改了你的 fiddle 。

我选择了同一范围内的父 div,然后使用您的 prevAll() 调用来精确我的选择。当您在实际项目中尝试此操作时会发生什么?

看看:

$(document).on('click', '.gmapInit', function() {
    var closeThing = $(this).parents("div.tableProcessingToolBarContainer").prevAll("div.googleMapsContainer:first");
    closeThing.attr("style", "");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="display: none;" class="googleMapsContainer" id="gm1">
        <div class="gmap" style="max-width: 80%;
            max-height: 400px;
            background-color: grey;" class="map">
        </div>
        This Text Should be visible after click
 </div>
 
 <div class="tableProcessingToolBarContainer">
      <div class="container-fluid tableProcessingTools" >
        <div class="row-fluid">
         <div class="appSliderContent tptSliderContent container-fluid ">
                <button class="gmapInit glassyButton">View data in Google Maps</button>
         </div>
        </div>
      </div>
    </div>
 
 <div style="display: none;" class="googleMapsContainer" id="gm2">
        <div class="gmap" style="max-width: 80%;
            max-height: 400px;
            background-color: grey;" class="map">
        </div>
        This Text Should be visible after click
 </div>

    <br>
    
    <div class="tableProcessingToolBarContainer">
      <div class="container-fluid tableProcessingTools" >
        <div class="row-fluid">
         <div class="appSliderContent tptSliderContent container-fluid ">
                <button class="gmapInit glassyButton">View data in Google Maps</button>
         </div>
        </div>
      </div>
    </div>

关于javascript - prevAll() 在更复杂的 DOM 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45842597/

相关文章:

javascript - 上面的 Div 另一个 JQuery 脚本不起作用!

html - px , em , % 是如何在 css 的 transform 属性中测量的?

html - 为什么移动版的 flex item 会溢出容器?

javascript - OnClick 的蓝色覆盖

jquery - 如何将数据从 PHP 返回到 jQuery ajax 调用

javascript - 如何使用 jQuery 在 div 之间添加文本?

javascript - 未捕获的语法错误 : missing ) after argument list javascript error

javascript - 用 jquery 交换不连续的元素?

javascript - 实现jQuery滚动效果的最佳方式

javascript - JQuery - 禁用表单,除非值不为 NULL?