附加div和类

时间:2016-11-08 21:24:34

标签: jquery html css

好的,所以我正在学习Jquery,但找不到我的问题的答案。我正在构建一个购物清单应用程序,无论您输入什么,它都应该附加到页面。它附加在word周围的类的类,但我不知道你将如何添加在同一div中的检查和删除框中。我尝试的每一种方式都将它添加到单独的div中,或者更改已经附加的属性。考虑到这对我来说很新,我可能没有清楚地解释清楚,但如果您需要更多信息,请问。谢谢。 https://github.com/Masonwharr/shoppinglist/tree/master/Shopping-List  https://i.stack.imgur.com/v0eky.png

2 个答案:

答案 0 :(得分:1)

基本上,要回答你的问题,你必须在单击按钮时写出你想要通过jQuery添加的所有html代码。当你点击"提交"你可以做这样的事情。它会调用下面的AddToshoppingList()函数:

function AddToShoppingList(itm) {
    var li += "<li><span class='shopping-item'>" + itm.Name + "</span>";
    li += "<div class='shopping-item-controls'>";
    li += "<button class='shopping-item-toggle'><span class='button-label'>check</span></button>";
    li += "<button class='shopping-item-delete'><span class='button-label'>delete</span></button>";
    li += "</div></li>";
    $("#ShoppingList").append(li);    
}

并将ID设置为<ul>列表,如下所示,以便您可以指定将新项目附加到的位置:

<ul id="ShoppingList" class="shopping-list">
... snip ...
</ul>

这应该让你开始,你可以自定义按钮,以便在点击这些按钮时调用其他jquery函数,如删除项目。

答案 1 :(得分:0)

不确定我是否理解正确。您可能需要详细解释,以便我们更好地为您提供帮助。我的想法是从页面获取html并在使用js将html追加到页面时使用它。 (它应该是一个模板,即使是页面上已有的模板......)

&#13;
&#13;
$(document).ready(function(){
  $('#js-shopping-list-form').submit(function(event){
    event.preventDefault();
      var inputfield = $('#entry').val();
      console.log(inputfield);
      $('.shopping-list').append('<li><span class="shopping-item">'+ inputfield + '</span>' + $('.shopping-item-controls').html() + '</li>');
      // This is incorrect, and adds an outside empty box. $('.shopping-list').append('<div class="shopping-item-controls"> <button class="shopping-item-toggle">' + '</button> </div>');
    //$('inputfield').appendTo('body').addClass('.shopping-item'); 
  });  
});
&#13;
* {
  box-sizing: border-box;
}

body {
  font-family: 'Roboto', sans-serif;
}

button, input[type="text"] {
  padding: 5px;
}

button:hover {
  cursor: pointer;
}

#shopping-list-item {
  width: 250px;
}

.container {
  max-width: 600px;
  margin: 0 auto;
}

.shopping-list {
  list-style: none;
  padding-left: 0;
}

.shopping-list > li {
  margin-bottom: 20px;
  border: 1px solid grey;
  padding: 20px;
}

.shopping-item {
  display: block;
  color: grey;
  font-style: italic;
  font-size: 20px;
  margin-bottom: 15px;
}

.shopping-item__checked {
  text-decoration: line-through;
}
&#13;
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Shopping List</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="js/script.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/4.2.0/normalize.min.css">
  <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
  <link rel="stylesheet" type="text/css" href="CSS/styles.css">
</head>
<body>
  <div class="container">
    <h1>Shopping List</h1>

    <form id="js-shopping-list-form">
      <label for="shopping-list-entry">Add an item</label>
      <input type="text" name="shopping-list-entry" placeholder="e.g., broccoli" id="entry">
      <button type="submit">Add item</button>
    </form>

    <ul class="shopping-list">
      <li>
        <span class="shopping-item">apples</span>
        <div class="shopping-item-controls">
          <button class="shopping-item-toggle">
            <span class="button-label">check</span>
          </button>
          <button class="shopping-item-delete">
            <span class="button-label">delete</span>
          </button>
        </div>
      </li>
      <li>
        <span class="shopping-item">oranges</span>
        <div class="shopping-item-controls">
          <button class="shopping-item-toggle">
            <span class="button-label">check</span>
          </button>
          <button class="shopping-item-delete">
            <span class="button-label">delete</span>
          </button>
        </div>
      </li>
      <li>
        <span class="shopping-item shopping-item__checked">milk</span>
        <div class="shopping-item-controls">
          <button class="shopping-item-toggle">
            <span class="button-label">check</span>
          </button>
          <button class="shopping-item-delete">
            <span class="button-label">delete</span>
          </button>
        </div>
      </li>
      <li>
        <span class="shopping-item">bread</span>
        <div class="shopping-item-controls">
          <button class="shopping-item-toggle">
            <span class="button-label">check</span>
          </button>
          <button class="shopping-item-delete">
            <span class="button-label">delete</span>
          </button>
        </div>
      </li>
    </ul>
  </div>

</body>
</html>
&#13;
&#13;
&#13;