为啥我的 Ajax 响应显示在我的导航栏上?

Posted

技术标签:

【中文标题】为啥我的 Ajax 响应显示在我的导航栏上?【英文标题】:Why does my Ajax response display over my navigation bar?为什么我的 Ajax 响应显示在我的导航栏上? 【发布时间】:2021-11-12 13:55:31 【问题描述】:

每当我滚动鼠标时,所有列出的管理卡都会出现在导航栏上。这是一个非常恼人的缺陷,我似乎无法理解我哪里出错了。任何帮助,将不胜感激。谢谢!

#mainsec 
  height: 100vh;
  width: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;

#navv 
  background-color: #333; /* Black background color */
  position: fixed; /* Make it stick/fixed */
  top: 0; /* Stay on top */
  width: 100%; /* Full width */
  transition: top 0.3s; /* Transition effect when sliding down (and up) */


#navv a:hover 
  background-color: rgba(221, 221, 221, 0.568);
  color: black;
<!DOCTYPE html>
<html lang="en">
<head>
  <!-- Required meta tags -->
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <!-- Bootstrap CSS -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous" />
  <link rel="stylesheet" type="text/css" href="../../css/admin_dash.css">

  <nav class="navbar navbar-expand-lg navbar-dark bg-dark" id="navv">
    <div class="container-fluid">
      <a class="navbar-brand" href="#">
        <img src="../../assets/logo.png"    class="d-inline-block align-text-top"> Phemesoft
      </a>
      <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
          </button>
      <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
        <div class="navbar-nav">
          <a class="nav-link active" aria-current="page" href="#sec1">Admins</a>
          <a class="nav-link" href="#sec2">Manage Admins</a>
          <a class="nav-link" href="#sec3">Course Content</a>
          <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
        </div>
      </div>
    </div>
  </nav>
  <div class="container">
    <div class="column">
      <div id="sec1">
        <div class="row" id="mainsec">
          <div class="col-sm-6">
            <h2>Admins</h2>
          </div>
          <div class="col-sm-6">
            <div class="row">
              <div class="col-sm-5 cards-wrapper"></div>
            </div>
          </div>
        </div>
      </div>
      <div id="sec2">
        <div class="row" id="mainsec">
          <div class="col-sm-6">
            <h1>User to Admin</h1>
          </div>
          <div class="col-sm-6">
            <form class="form" action="makeadmin" method="POST">
              Enter email:
              <input type="email" placeholder="Email" name="email">
              <button type="submit" class="btn btn-light">Check</button>
            </form>
          </div>
        </div>
      </div>
      <div id="sec3">
        <div class="col-sm-6" id="mainsec">
          <h1>Add course content</h1>
        </div>
      </div>
    </div>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-/bQdsTh/da6pkI1MST/rWKFNjaCP5gBSY4sEBT38Q/9RBh9AH40zEOg7Hlq2THRZ" crossorigin="anonymous"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
  <script>
    $.ajax(
      url: "listadmins",
      type: "GET",
      success: function(response) 
        var $results = $(".cards-wrapper");
        for (var a = 0; a < response.length; a++) 
          // console.log(response[a].Name);
          $results.append(
            '<div class="card"><div class="card-body"><h6 class="card-title">' +
            response[a].Name +
            '</h6></div></div>'
          );
        
      
    );
  </script>
  <script>
    var prevScrollpos = window.pageYOffset;
    window.onscroll = function() 
      var currentScrollPos = window.pageYOffset;
      if (prevScrollpos > currentScrollPos) 
        document.getElementById("navv").style.top = "0";
       else 
        document.getElementById("navv").style.top = "-50px";
      
      prevScrollpos = currentScrollPos;
    ;
  </script>
  </body>
</html>

这是问题的图片: The card appears over then nav bar

Ps:我没有发布 /listadmins 的映射,因为它只是普通的数据库查询

【问题讨论】:

它是 ajax 响应这一事实与您的问题无关。 (这只是一个 html/css 问题) 在导航中使用更高的 z-index。 @disinfor 有些东西没有关闭/额外,但您声明的 div 或 nav 并非如此 @MarkSchultheiss 是的!我完全错过了一个开头元素。 【参考方案1】:

从卡片类中移除'position: relative'

【讨论】:

【参考方案2】:

这与 ajax 直接无关,而与页面的其余部分有关。

您的 html 无效

head 标签未关闭 正文标签未打开 重复的 id mainsec - 没有真正使用它们,所以我将它们设为一个类并删除了 id 其他一些无效标记

这里很难看出问题,所以我稍微调整了这个网站的大小

现在我们已经解决了这个问题,您可以根据需要使用mt-5 pt-4 调整顶部的边距和内边距,玩转这个,看看您要移动的位置。

.mainsec 
  height: 5em;
  width: 90%;
  display: flex;
  align-items: center;
  justify-content: center;


#navv 
  background-color: #333;
  position: fixed;
  top: 0;
  width: 100%;
  transition: top 0.3s;


#navv a:hover 
  background-color: rgba(221, 221, 221, 0.568);
  color: black;
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous" />
  <link rel="stylesheet" type="text/css" href="../../css/admin_dash.css">
</head>

<body>
  <nav class="navbar navbar-expand-lg navbar-dark bg-dark" id="navv">
    <div class="container-fluid">
      <a class="navbar-brand" href="#">
        <img src="../../assets/logo.png"   class="d-inline-block align-text-top" >
      </a>
      <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
          </button>
      <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
        <div class="navbar-nav">
          <a class="nav-link active" aria-current="page" href="#sec1">Admins</a>
          <a class="nav-link" href="#sec2">Manage Admins</a>
          <a class="nav-link" href="#sec3">Course Content</a>
          <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
        </div>
      </div>
    </div>
  </nav>
  <div class="container mt-6">
    <div class="">
      <div id="sec1">
        <div class="mainsec mt-5">
          <div class="col-sm-3">
            <h2>Admins</h2>
          </div>
          <div class="col-sm-7 mt-5 pt-4">
            <div class="row">
              <div class="col-sm-8 cards-wrapper">
                <div class="cards-wrapper">
                  <div class="card">
                    <div class="card-body">
                      <h6 class="card-title">Pretend append
                      </h6>
                    </div>
                  </div>
                  <div class="card">
                    <div class="card-body">
                      <h6 class="card-title">Pretend append
                      </h6>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="row" id="sec2">
        <div class="row mainsec">
          <div class="col-sm-6">
            <h1>User to Admin</h1>
          </div>
          <div class="col-sm-6">
            <form class="form" action="makeadmin" method="POST">
              Enter email:
              <input type="email" placeholder="Email" name="email">
              <button type="submit" class="btn btn-light">Check</button>
            </form>
          </div>
        </div>
      </div>
      <div class="row"  id="sec3">
        <div class="col-sm-6 mainsec">
          <h1>Add course content</h1>
        </div>
      </div>
    </div>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-/bQdsTh/da6pkI1MST/rWKFNjaCP5gBSY4sEBT38Q/9RBh9AH40zEOg7Hlq2THRZ" crossorigin="anonymous"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
  <script>
    $.ajax(
      url: "listadmins",
      type: "GET",
      success: function(response) 
        var $results = $(".cards-wrapper");
        for (var a = 0; a < response.length; a++) 
          $results.append(
            '<div class="card"><div class="card-body"><h6 class="card-title">' +
            response[a].Name +
            '</h6></div></div>'
          );
        
      
    );

    var prevScrollpos = window.pageYOffset;
    window.onscroll = function() 
      var currentScrollPos = window.pageYOffset;
      if (prevScrollpos > currentScrollPos) 
        document.getElementById("navv").style.top = "0";
       else 
        document.getElementById("navv").style.top = "-50px";
      
      prevScrollpos = currentScrollPos;
    ;
  </script>
</body>

</html>

【讨论】:

嘿,是的,我完全忘记了标签,但我正在使用 mainsec,将其分成多个部分。就像当你点击导航栏时它会向下滚动到一个新页面,并且 mainsec 提供了高度和宽度以适应屏幕上的一个内容。【参考方案3】:

感谢大家的回复。我在 cmets 中找到了答案。这是为导航栏使用更高的 z-index 值。

#navv 
  background-color: #333; /* Black background color */
  position: fixed; /* Make it stick/fixed */
  top: 0; /* Stay on top */
  width: 100%; /* Full width */
  transition: top 0.3s; /* Transition effect when sliding down (and up) */
  z-index: 2;

【讨论】:

这可能意味着您的文档无法正常流动恕我直言

以上是关于为啥我的 Ajax 响应显示在我的导航栏上?的主要内容,如果未能解决你的问题,请参考以下文章

无法在导航栏上添加导航项

为啥导航栏列表不在右侧?

无法同时在我的标题和导航栏上使用 justify-content 和 align-items

如何在视图控制器中的标签栏和导航栏上全屏显示弹出视图?

我无法让我的徽标显示在导航栏上

Swift 3 - 为啥我的导航栏没有显示?