javascript - 单击时列表项消失

标签 javascript jquery html css

好的,所以我正在尝试为一些随机生成的人构建一个叠加层。这是我正在处理的一个元素,您可以使用一些 jQuery 和 AJAX 动态请求随机用户,我已经记下并列出了它们,但我需要能够单击它们,它们会像模态窗口一样显示。这就是问题所在。当您单击它们时,我会显示它们,但它们会在页面上消失。是什么原因造成的,我该如何解决?忽略模式的节奏我不担心 CSS rn。

let html = "";

$.ajax({
  url: 'https://randomuser.me/api/?results=5000',
  dataType: 'json',
  success: function(data) {
    html += "<ul>"
    for (i = 0; i < 12; i++) {

      html += "<li>";
      html += "<img src=" + '"' + data.results[i].picture.medium + '">';
      html += "<div id='main'>"
      html += "<h3>" + data.results[i].name.first + " " + data.results[i].name.last + "</h3>";
      html += "<p>" + data.results[i].email + "</p>";
      html += "<p>" + data.results[i].location.city + "</p>";
      html += "<p class='popup'>" + data.results[i].phone + "</p>"
      html += "<p class='popup'>" + data.results[i].location.street + ", " + data.results[i].location.state + " " + data.results[i].location.postcode + "</p>";
      html += "<p class='popup'>" + data.results[i].dob + "</p>"
      html += "</div>";
      html += "</li>";
    }
    html += "</ul>"
    $("#employeeList").html(html);

    $("li").on("click", function() {
      $(".overlay").css("display", "block");
      $(".modal").html(this);
      $(".modal").preventDefault();
    });

    $(".overlay").on("click", function() {
      $(".overlay").css("display", "none");
    });

  }
});
* {
  list-style-type: none;
  text-decoration: none;
}

body,
html {
  background-color: #eff2f5;
  margin: 0;
}

ul {
  display: flex;
  flex-wrap: wrap;
  flex: 1;
  margin-right: 25px;
}

.popup {
  display: none;
}

li img {
  width: 110px;
  height: 110px;
  border-radius: 50%;
  margin-right: 15px;
  margin-left: 10px;
  margin-top: 10px;
  margin-bottom: 10px;
}

li {
  flex: 1;
  display: flex;
  flex-direction: row;
  justify-content: center;
  background-color: white;
  border-radius: 5px;
  margin: 12px;
  border: 2px lightgray solid;
  min-width: 400px;
}

li:hover {
  cursor: pointer;
}

.overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 10;
  background-color: rgba(0, 0, 0, 0.5);
  display: none;
}

p {
  color: gray;
  margin: 5px;
  margin-left: 0;
}

#main {
  margin-bottom: 10px;
}

h3 {
  margin-bottom: 0;
}

.modal {
  background-color: grey;
  z-index: 20;
  position: absolute;
  top: 40%;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.1.25/jquery.fancybox.min.css">
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.1.25/jquery.fancybox.min.js"></script>
<div class="overlay">
  <div class="modal"></div>
</div>
<h1>Awesome Startup Employee Directory</h1>
<div id="employeeList"></div>

抱歉所有内容,但我卡住了。提前致谢!!

最佳答案

这是因为你将“this”传递给模态,所以我认为最好只复制这样的内容

    $("li").on("click", function () {
      var $li = '<li>' + this.innerHTML +'<li>';
      $(".overlay").css("display", "block");
      $(".modal").html($li);
      //$(".modal").preventDefault();
    });

let html = ""; 

$.ajax({
  url: 'https://randomuser.me/api/?results=5000',
  dataType: 'json',
  success: function(data) {
    html += "<ul>"
    for(i = 0; i < 12; i++) {

       html += "<li>";
       html += "<img src=" + '"' + data.results[i].picture.medium + '">';
       html += "<div id='main'>"
       html += "<h3>" + data.results[i].name.first + " " + data.results[i].name.last + "</h3>";
       html += "<p>" + data.results[i].email + "</p>";
       html += "<p>" + data.results[i].location.city + "</p>";
       html += "<p class='popup'>" + data.results[i].phone + "</p>"
       html += "<p class='popup'>" + data.results[i].location.street + ", " + data.results[i].location.state + " " + data.results[i].location.postcode + "</p>";
       html += "<p class='popup'>" + data.results[i].dob + "</p>"
       html += "</div>";
       html += "</li>";
    }   
      html += "</ul>"
      $("#employeeList").html(html);

    $("li").on("click", function () {
      var $li = '<li>' + this.innerHTML +'<li>';
     $(".overlay").css("display", "block");
     $(".modal").html($li);
     //$(".modal").preventDefault();
  });

    $(".overlay").on("click", function () {
     $(".overlay").css("display", "none");
  });

  }
});
* {
    list-style-type: none;
    text-decoration: none;
}

body, html {
    background-color: #eff2f5;
    margin: 0;
}


ul {
    display: flex;
    flex-wrap: wrap;
    flex: 1;
    margin-right: 25px;

}

.popup {
    display: none;
}

li img {
    width: 110px;
    height: 110px;
    border-radius: 50%;
    margin-right: 15px;
    margin-left: 10px;
    margin-top: 10px;
    margin-bottom: 10px;
}


li {
   flex: 1; 
   display: flex;
   flex-direction: row;
   justify-content: center;
   background-color: white;
   border-radius: 5px;
   margin: 12px;
   border: 2px lightgray solid;
   min-width: 400px;
}

li:hover {
    cursor: pointer;
}

.overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 10;
  background-color: rgba(0,0,0,0.5);
  display: none;
}

p {
    color: gray;
    margin: 5px;
    margin-left: 0;
}

#main {
    margin-bottom: 10px;
}

h3 {
    margin-bottom: 0;
}

.modal {
    background-color: grey;
    z-index: 20;
    position: absolute;
    top: 40%;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>AJAX Project</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.1.25/jquery.fancybox.min.css">
    <link rel="stylesheet" href="css/main.css">
</head>
<body>
<div class="overlay">
     <div class="modal"></div>
</div>
 <h1>Awesome Startup Employee Directory</h1>
 <div id="employeeList"></div>
  <script
  src="https://code.jquery.com/jquery-3.2.1.min.js"
  integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
  crossorigin="anonymous"></script> 
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.1.25/jquery.fancybox.min.js"></script>
  <script src="js/main.js"></script>    
 </body>
</html>

关于javascript - 单击时列表项消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46385612/

相关文章:

javascript - 根据数据属性的键值选择元素

javascript - 在这种情况下为什么要使用 call() 方法?

javascript - 尝试将变量状态传递给服务器

javascript - 脚本不适用于博客上的 cloudflare ssl,但在 https 关闭时有效

jquery - 如何将搜索框放在轮播的顶部?

javascript - 如何从div的样式覆盖css

javascript - 在javascript中调用带有双参数()的函数

javascript - 如果重新选择选项,如何停止 jquery 函数继续重复

javascript改变文本框的颜色

php - 将值从 html 表保存到数据库