jquery - 使用 jQuery 将联系信息添加到新的 div

标签 jquery html css dynamic

我正在开发一个元素,用户可以在其中输入名字、姓氏和简短描述。点击提交按钮后,名字和姓氏将转移到下面的新 div 中。如果您单击名字和姓氏,新的 div 将翻转并显示用户为自己写的描述。如果用户单击描述,卡片将翻回名字和姓氏。 点击提交按钮后,我可以获得要转移的名字和姓氏。但是,我很难让带有名字和姓氏的卡片翻转并显示描述信息并翻转回名字和姓氏。如有任何建议,我们将不胜感激。

$(document).ready(function() {
  alert('jQuery activated');

  $('form').submit(function() {
    alert('form submitted');
    $('#contact_card').append("<div class='person'><h4>" + $('#first_name').val() + " " + $('#last_name').val() + "</h4><p>Click for more details</p></div>");

    return false;


  });

  $(document).on('click', '.person', function() {
    alert('user clicked');
    $(this).children().toggle('slow');




  });

});
* {
  font-family: "Tahoma";
  margin: 0px;
  padding: 0px;
}
#container {
  width: 1000px;
  height: 750px;
  margin: 0px auto;
}
#left,
form {
  display: inline-block;
  max-width: 500px;
}
#left form h3 {
  margin: 0px 0px 13px 0px;
}
.person {
  border: 8px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<head>
  <title>Advanced jQuery Assignment III: Contact Card</title>

</head>

<body>
  <div id="container">
    <div id="left">
      <form>
        <br>First Name:
        <input class="user_input" type="text" id="first_name">
        <br>
        <br>Last Name:
        <input class="user_input" type="text" id="last_name">
        <br>
        <label for="description"></label>
        <textarea name="description" type="text" id="description" cols="50" rows="10">Enter Description</textarea>
        <br>
        <input type="submit" value="Add User">
      </form>
    </div>
    <!-- end of left -->

    <div id="bottom">
      <div id="contact_card">

      </div>
      <!-- end of contact_card -->
    </div>
    <!-- end of bottom -->
  </div>
  <!-- end of container -->

最佳答案

因此在隐藏的p 中将描述添加到contact_card。然后,当您单击类 person 时,只需切换 p 子级即可。

$(document).ready(function() {

  $('form').submit(function() {
    $('#contact_card').append("<div class='person'><h4>" + $('#first_name').val() + " " + $('#last_name').val() + "</h4><p>Click for more details</p><p hidden>"+$('#description').val()+"</p></div>");

    return false;
  });

  $(document).on('click', '.person', function() {
    $(this).children('p').toggle('slow');
  });

});
* {
  font-family: "Tahoma";
  margin: 0px;
  padding: 0px;
}
#container {
  width: 1000px;
  height: 750px;
  margin: 0px auto;
}
#left,
form {
  display: inline-block;
  max-width: 500px;
}
#left form h3 {
  margin: 0px 0px 13px 0px;
}
.person {
  border: 8px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<head>
  <title>Advanced jQuery Assignment III: Contact Card</title>

</head>

<body>
  <div id="container">
    <div id="left">
      <form>
        <br>First Name:
        <input class="user_input" type="text" id="first_name">
        <br>
        <br>Last Name:
        <input class="user_input" type="text" id="last_name">
        <br>
        <label for="description"></label>
        <textarea name="description" type="text" id="description" cols="50" rows="10">Enter Description</textarea>
        <br>
        <input type="submit" value="Add User">
      </form>
    </div>
    <!-- end of left -->

    <div id="bottom">
      <div id="contact_card">

      </div>
      <!-- end of contact_card -->
    </div>
    <!-- end of bottom -->
  </div>
  <!-- end of container -->

关于jquery - 使用 jQuery 将联系信息添加到新的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39665259/

相关文章:

jquery - 使用 jQuery 的 .css() 会覆盖 CSS 中的伪类吗?

jquery - 禁用具有超时功能的按钮使用

javascript - 单击按钮不单击到表行

javascript - 单击任何元素并在存在时读取特定 HTML-5 自定义属性的监听器?

html - 如何在 HTML 中的 <td> 之后和 <table> 中创建换行符?

javascript - Metro UI 代码不起作用

javascript - 使用 jQuery 防止事件进一步传播

css - SMACSS:在模块内使用网格是否正确?

html - 将 div 定位在容器底部

jquery - 扩展菜单显示扩展前的文本